ListView: Cell Swipe Gesture

The swipe gesture feature allows end-users to use swipe gesture on cells. When the user swipes, the content view moves revealing a designated swipe background view where you can place custom views ready for interaction e.g. buttons images etc.

Enabling cell swipe gesture

Use the allowsCellSwipe property to allow the user to perform swipe gesture on cells.

_listView.allowsCellSwipe = YES;
listView.allowsCellSwipe = true
this.listView.AllowsCellSwipe = true;

Configuring cell swipe gesture

Use the cellSwipeLimits property to set how far the cell may be swiped.

_listView.cellSwipeLimits = UIEdgeInsetsMake(0, 60, 0, 180);
listView.cellSwipeLimits = UIEdgeInsetsMake(0, 60, 0, 180)
this.listView.CellSwipeLimits = new UIEdgeInsets (0, 60, 0, 180);

Use the cellSwipeTreshold property to set the minimum distance the user needs to swipe before the gesture is considered effective. If the user swipes below the treshold, the cell will auto-revert to its original state.

_listView.cellSwipeTreshold = 30;
listView.cellSwipeTreshold = 30
this.listView.CellSwipeTreshold = 30;

Use the cellSwipeAnimationDuration property to set the cell swipe animation duration

Add the content that should be visible when swipe is applied to the backgroundView of TKListViewCell:

if (cell.swipeBackgroundView.subviews.count == 0) {

    CGSize size = cell.frame.size;
    UIFont *font = [UIFont systemFontOfSize:14];

    UIButton *bMore = [[UIButton alloc] initWithFrame:CGRectMake(size.width - 180, 0, 60, size.height)];
    [bMore setTitle:@"More" forState:UIControlStateNormal];
    [bMore setBackgroundColor:[UIColor lightGrayColor]];
    bMore.titleLabel.font = font;
    [bMore addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
    [cell.swipeBackgroundView addSubview:bMore];

    UIButton *bFlag = [[UIButton alloc] initWithFrame:CGRectMake(size.width - 120, 0, 60, size.height)];
    [bFlag setTitle:@"Flag" forState:UIControlStateNormal];
    [bFlag setBackgroundColor:[UIColor orangeColor]];
    bFlag.titleLabel.font = font;
    [bFlag addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
    [cell.swipeBackgroundView addSubview:bFlag];

    UIButton *bTrash = [[UIButton alloc] initWithFrame:CGRectMake(size.width - 60, 0, 60, size.height)];
    [bTrash setTitle:@"Trash" forState:UIControlStateNormal];
    [bTrash setBackgroundColor:[UIColor redColor]];
    bTrash.titleLabel.font = font;
    [bTrash addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
    [cell.swipeBackgroundView addSubview:bTrash];

    UIButton *bUnread = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, size.height)];
    [bUnread setTitle:@"Mark as Unread" forState:UIControlStateNormal];
    [bUnread setBackgroundColor:[UIColor blueColor]];
    bUnread.titleLabel.font = font;
    bUnread.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    bUnread.titleLabel.textAlignment = NSTextAlignmentCenter;
    [bUnread addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
    [cell.swipeBackgroundView addSubview:bUnread];
}
if(cell.swipeBackgroundView.subviews.count == 0){
    let size = cell.frame.size
    let font = UIFont.systemFont(ofSize: 14)

    let bMore = UIButton()
    bMore.frame = CGRect(x: size.width - 180, y: 0, width: 60, height: size.height)
    bMore.setTitle("More", for: UIControlState())
    bMore.backgroundColor = UIColor.lightGray
    bMore.titleLabel?.font = font
    bMore.addTarget(self, action: #selector(ListViewSwipe.buttonTouched), for: UIControlEvents.touchUpInside)
    cell.swipeBackgroundView.addSubview(bMore)

    let bFlag = UIButton()
    bFlag.frame = CGRect(x: size.width - 120, y: 0, width: 60, height: size.height)
    bFlag.setTitle("Flag", for: UIControlState())
    bFlag.backgroundColor = UIColor.orange
    bFlag.titleLabel?.font = font
    bFlag.addTarget(self, action: #selector(ListViewSwipe.buttonTouched), for: UIControlEvents.touchUpInside)
    cell.swipeBackgroundView.addSubview(bFlag)

    let bTrash = UIButton()
    bTrash.frame = CGRect(x: size.width - 60, y: 0, width: 60, height: size.height)
    bTrash.setTitle("Trash", for: UIControlState())
    bTrash.backgroundColor = UIColor.red
    bTrash.titleLabel?.font = font
    bTrash.addTarget(self, action: #selector(ListViewSwipe.buttonTouched), for: UIControlEvents.touchUpInside)
    cell.swipeBackgroundView.addSubview(bTrash)

    let bUnread = UIButton()
    bUnread.frame = CGRect(x: 0, y: 0, width: 60, height: size.height)
    bUnread.setTitle("Mark as unread", for: UIControlState())
    bUnread.backgroundColor = UIColor.blue
    bUnread.titleLabel?.font = font
    bUnread.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
    bUnread.titleLabel?.textAlignment = NSTextAlignment.center
    bUnread.addTarget(self, action: #selector(ListViewSwipe.buttonTouched), for: UIControlEvents.touchUpInside)
    cell.swipeBackgroundView.addSubview(bUnread)

}
if (cell.SwipeBackgroundView.Subviews.Length == 0) {
    CGSize size = cell.Frame.Size;
    UIFont font = UIFont.SystemFontOfSize(14);
    UIButton bMore = new UIButton(new CGRect(size.Width - 180, 0, 60, size.Height));
    bMore.SetTitle("More", UIControlState.Normal);
    bMore.BackgroundColor = UIColor.LightGray;
    bMore.TitleLabel.Font = font;
    bMore.AddTarget(ButtonTouched, UIControlEvent.TouchUpInside);
    cell.SwipeBackgroundView.AddSubview(bMore);

    UIButton bFlag = new UIButton(new CGRect(size.Width - 120, 0, 60, size.Height));
    bFlag.SetTitle("Flag", UIControlState.Normal);
    bFlag.BackgroundColor = UIColor.Orange;
    bFlag.TitleLabel.Font = font;
    bFlag.AddTarget(ButtonTouched, UIControlEvent.TouchUpInside);
    cell.SwipeBackgroundView.AddSubview(bFlag);

    UIButton bTrash = new UIButton(new CGRect(size.Width - 60, 0, 60, size.Height));
    bTrash.SetTitle("Trash", UIControlState.Normal);
    bTrash.BackgroundColor = UIColor.Red;
    bTrash.TitleLabel.Font = font;
    bTrash.AddTarget(ButtonTouched, UIControlEvent.TouchUpInside);
    cell.SwipeBackgroundView.AddSubview(bTrash);

    UIButton bUnread = new UIButton(new CGRect(0, 0, 60, size.Height));
    bUnread.SetTitle("Mark as Unread", UIControlState.Normal);
    bUnread.BackgroundColor = UIColor.Blue;
    bUnread.TitleLabel.Font = font;
    bUnread.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap;
    bUnread.TitleLabel.TextAlignment = UITextAlignment.Center;
    bUnread.AddTarget(ButtonTouched, UIControlEvent.TouchUpInside);
    cell.SwipeBackgroundView.AddSubview(bUnread);
}

Responding to swipe interactions

In order to respond programmatically to a swipe gesture performed by user, you will need to implement one or more of the following methods from the TKListViewDelegate protocol.

- (void)listView:(TKListView *)listView didSwipeCell:(TKListViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withOffset:(CGPoint)offset
{
    [self animateButtonsInCell:cell widthOffset:offset];
}

- (void)listView:(TKListView *)listView didFinishSwipeCell:(TKListViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withOffset:(CGPoint)offset
{
    NSLog(@"swiped cell at index path: %ld", (long)indexPath.row);
}
func listView(_ listView: TKListView, didSwipeCell cell: TKListViewCell, at indexPath: IndexPath, withOffset offset: CGPoint) {
    animateButtonsInCell(cell, offset: offset)
}

func listView(_ listView: TKListView, didFinishSwipeCell cell: TKListViewCell, at indexPath: IndexPath, withOffset offset: CGPoint) {
    print("Swiped cell at indexPath: %d", (indexPath as NSIndexPath).row)
}
class ListViewDelegate: TKListViewDelegate 
{
    ListViewSwipe owner;

    public ListViewDelegate(ListViewSwipe owner) 
    {
        this.owner = owner;
    }

    public override void DidSwipeCell (TKListView listView, TKListViewCell cell, NSIndexPath indexPath, CGPoint offset)
    {
        this.owner.AnimateButtonInCell (cell, offset);
    }

    public override void DidFinishSwipeCell (TKListView listView, TKListViewCell cell, NSIndexPath indexPath, CGPoint offset)
    {
        Console.WriteLine ("Did swipe cell at index: {0}", indexPath.Row);
    }
}