hbk
hbk

Reputation: 11184

How to move cell in tableViewCell with animation with usage of swipe action

using: objective-C

I have a tableView with rows. I want that user can move cell a little aside and additional action shown to him.

What was done:

Currently create separately table in xib file and cell in xib file. Cell is very simple enter image description here

I want to move viewWIthLabel. In cell class file for using animation I use next code

- (void)awakeFromNib
{
    UISwipeGestureRecognizer * leftGestrudeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
leftGestrudeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    UISwipeGestureRecognizer * rightGestrudeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
rightGestrudeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self addGestureRecognizer:rightGestrudeRecognizer];
[self addGestureRecognizer:leftGestrudeRecognizer];
}

and actions:

- (IBAction)swipeLeft:(id)sender{
NSLog(@"swipeL");
[UIView animateWithDuration:0.5 animations:^{
    self.viewWIthLabel.frame = CGRectMake(-100, 0, self.viewWIthLabel.frame.size.width, self.viewWIthLabel.frame.size.height);
} ];
}

 - (IBAction)swipeRight:(id)sender{
NSLog(@"swipeR");
[UIView animateWithDuration:.5 animations:^{
    self.viewWIthLabel.frame = CGRectMake(0, 0, self.viewWIthLabel.frame.size.width, self.viewWIthLabel.frame.size.height);
}];
}

So idea to swipe the cell and move it with animation for some distance to show a hidden button.

Result of this code - almoust like I want:

enter image description hereenter image description here

But if you start to scroll the tableView you can get duplicates of "swiped cells" in random (depending on scrolling speed) positions:

enter image description here

Any idea why it happened?

Upvotes: 0

Views: 344

Answers (1)

Vikram Rao
Vikram Rao

Reputation: 514

You might have to clear/reset the swiped state of the cell in – tableView:cellForRowAtIndexPath:. Since the cells get re-used (I am assuming you are doing that as would be required for good performance of the tableview). Probably something like this -

 self.viewWIthLabel.frame = CGRectMake(0, 0, self.viewWIthLabel.frame.size.width, self.viewWIthLabel.frame.size.height);

Upvotes: 2

Related Questions