user1191140
user1191140

Reputation: 1579

Implement an app icon like behavior

I want to implement an app icon (on home screen) like behavior. So I need two functionality in there.

  1. Wobble on Long Press and display a delete icon
  2. Press the delete icon and remove the view.

I could get No 1 correctly, via this person of code.

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [longPress setMinimumPressDuration:1];
    [view addGestureRecognizer:longPress];

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateBegan ) {
        CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5));
        CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5.0));
        view.transform = leftWobble;  // starting point


        UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];

        deleteButton.frame = CGRectMake(-4, 0, 43, 54);
        [view addSubview: deleteButton];
        [view bringSubviewToFront:deleteButton];
        [deleteButton setBackgroundImage:[UIImage imageNamed:@"trashcan.png"] forState:UIControlStateNormal];
        [deleteButton addTarget:self action:@selector(deletePressed:) forControlEvents:UIControlEventTouchUpInside];

        [UIView animateWithDuration:0.5 delay:0 options:( UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut) animations:^{
            view.transform = rightWobble;
        }completion:^(BOOL finished){
            view.transform = CGAffineTransformIdentity;
        }];
    }
}

Now I expected the deletePressed to get triggered when the view is wobbly and delete button is pressed. But that never gets trigger. Please help me find out what is going wrong.

Upvotes: 1

Views: 112

Answers (2)

Apurv
Apurv

Reputation: 17186

While providing animation you should add the option of user interaction.

[UIView animateWithDuration:0.5 delay:0 options:
     (UIViewAnimationOptionAutoreverse|
      UIViewAnimationOptionRepeat|
      UIViewAnimationOptionCurveEaseInOut| 
      UIViewAnimationOptionAllowUserInteraction) 
      animations:^{
            view.transform = rightWobble;
            }completion:^(BOOL finished){
                 view.transform = CGAffineTransformIdentity;
            }];

Upvotes: 1

Michael
Michael

Reputation: 1107

I was breaking my head over this because it didn't make sense to me. Then I noticed the code was working prefect until the UIView animateWithDuration block.

Apparently there is also a UIViewAnimationOptionAllowUserInteraction option which is set to NO by default. Change your code to this and you should be fine:

[UIView animateWithDuration:0.2 delay:0 options:( UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction) animations:^{
            view.transform = rightWobble;
        }completion:^(BOOL finished){
            view.transform = CGAffineTransformIdentity;
        }];

Also if it still doesn't work and your view is an UIImageView then set view.userInteractionEnabled = YES

My working code looked like this: totalcode

Upvotes: 1

Related Questions