MortimerGoro / MGSwipeTableCell

An easy to use UITableViewCell subclass that allows to display swippable buttons with a variety of transitions.
MIT License
6.96k stars 1.07k forks source link

how to disable multi swipe at one time #247

Open buvtopcc opened 8 years ago

buvtopcc commented 8 years ago

as title describe

skibadawid commented 7 years ago

Kinda got it to work. I only cared about right sided buttons, so made changes to handle just that situation. Also for MailAppDemoSwift demo project. I ended up adding logic to MailViewController.swift and made few changes to MGSwipeTableCell.m file.

for MailViewController.swift add this to the top, below other variables

var swipedCell: UITableViewCell?

remove the deprecated method

func swipeTableCell(_ cell: MGSwipeTableCell, canSwipe direction: MGSwipeDirection) -> Bool {}

add these, they are with logs so you can see what is called when

func swipeTableCell(_ cell: MGSwipeTableCell, canSwipe direction: MGSwipeDirection, from point: CGPoint) -> Bool {
    if direction == .leftToRight { return false }  // only care about .rightToLeft

    if self.swipedCell == nil {
        self.swipedCell = cell
        if let indexPath = self.tableView.indexPath(for: cell) {
            print ("allow swipe row \(indexPath.row) - \(direction == .leftToRight ? "leftToRight" : "rightToLeft")")
        }
        return true
    }
    else if self.swipedCell == cell {    // since when cell calls this delegate method, if check for both swipe directions
        if let indexPath = self.tableView.indexPath(for: cell) {
            print ("allow swipe row \(indexPath.row) - \(direction == .leftToRight ? "leftToRight" : "rightToLeft")")
        }
        return true
    }

    if let indexPath = self.tableView.indexPath(for: cell) {
        print ("NAAAH to swipe row \(indexPath.row) - \(direction == .leftToRight ? "leftToRight" : "rightToLeft")")
    }
    return false
}

func swipeTableCellWillEndSwiping(_ cell: MGSwipeTableCell) {
    self.swipedCell = nil
    if let indexPath = self.tableView.indexPath(for: cell) {
        print ("   end swiping row \(indexPath.row)")
    }
}

func swipeTableCellWillBeginSwiping(_ cell: MGSwipeTableCell) {
    if let indexPath = self.tableView.indexPath(for: cell) {
        print ("   begin swiping row \(indexPath.row)")
    }
}

func swipeTableCell(_ cell: MGSwipeTableCell, tappedButtonAt index: Int, direction: MGSwipeDirection, fromExpansion: Bool) -> Bool {
    self.swipedCell = nil
    if let indexPath = self.tableView.indexPath(for: cell) {
        print("row \(indexPath.row) colapse: [button at index\(index)] from exp: \(fromExpansion)")
    }
    return true
}

And for MGSwipeTableCell.m, I replaced the code after the "make a decision according to existing buttons..." (about line 1310 tip 1329) with one below. Replaced it since it did two swipeTableCell:canSwipe:fromPoint delegate calls per cell for both MGSwipeDirection, even though it that should never be the case. Those two delegate calls messed with logic I added to MailViewController.swift file.

if (translation.x > 0) {
    if (_delegate && [_delegate respondsToSelector:@selector(swipeTableCell:canSwipe:fromPoint:)]) {
        CGPoint point = [_panRecognizer locationInView:self];
        _allowSwipeLeftToRight = [_delegate swipeTableCell:self canSwipe:MGSwipeDirectionLeftToRight fromPoint:point];
    }
    else if (_delegate && [_delegate respondsToSelector:@selector(swipeTableCell:canSwipe:)]) {
        #pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        _allowSwipeLeftToRight = [_delegate swipeTableCell:self canSwipe:MGSwipeDirectionLeftToRight];
        #pragma clang diagnostic pop
    }
    else {
        [self fetchButtonsIfNeeded];
        _allowSwipeLeftToRight = _leftButtons.count > 0;
    }
    return _allowSwipeLeftToRight;
}
else if (translation.x < 0) {
    if (_delegate && [_delegate respondsToSelector:@selector(swipeTableCell:canSwipe:fromPoint:)]) {
        CGPoint point = [_panRecognizer locationInView:self];
        _allowSwipeRightToLeft = [_delegate swipeTableCell:self canSwipe:MGSwipeDirectionRightToLeft fromPoint:point];
    }
    else if (_delegate && [_delegate respondsToSelector:@selector(swipeTableCell:canSwipe:)]) {
        #pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        _allowSwipeRightToLeft = [_delegate swipeTableCell:self canSwipe:MGSwipeDirectionRightToLeft];
        #pragma clang diagnostic pop
    }
    else {
        [self fetchButtonsIfNeeded];
        _allowSwipeRightToLeft = _rightButtons.count > 0;
    }

    return _allowSwipeRightToLeft;
 }
 else {
    _allowSwipeLeftToRight = NO;
    _allowSwipeRightToLeft = NO;
    return NO;
}

Hopefully this didn't break anything.

ecnepsnai commented 7 years ago

Any thoughs @MortimerGoro ?

icaromag commented 6 years ago

Hey, any updates on this?