Apollo
Apollo

Reputation: 9064

Strikethrough Text with Animation iOS

What is the best way to display a strikethrough animation? When a user swipes their finger across a UITableViewCell, I would like to animate a thin line across cell.textlabel.text

The two ways I've thought of so far would be using Animation or somehow displaying a custom image and revealing it slowly from left to right? Does anybody have any advice on this?

I already have the swipe gestures working, I now just need to know how to make the animation happen:

Add Gesture Recognizer:

  //Add a left swipe gesture recognizer
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                                                     action:@selector(handleSwipeLeft:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.tableView addGestureRecognizer:recognizer];

    //Add a right swipe gesture recognizer
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                                           action:@selector(handleSwipeRight:)];
    recognizer.delegate = self;
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.tableView addGestureRecognizer:recognizer];

Delegate Methods for Gestures:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
     NSLog(@"uncompleted");
}

// Cross Item off of the list
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
    NSLog(@"completed");
}

Upvotes: 3

Views: 971

Answers (1)

Ralph Caraveo
Ralph Caraveo

Reputation: 10225

Based on what you have so far the following should work:

  1. when handleSwipeLeft (or right) fires, place a new UIView with a black background color over the textfield at the textfield's x point and around halfway to the y point with a width of 0 and height of 1
  2. then, call [UIView animationWithDuration....] changing the UIView's width property to be roughly the width of the textfield.

This should be close to what you want with some tweaking. I don't think it will be possible to animate the strikethrough from using the properties of the font alone but this technique should simulate it just fine.

Good job on getting half way there.

Upvotes: 2

Related Questions