How to stop an UIView animation

2019-03-09 01:33发布

问题:

I have several UIImageViews within a UIScrollView that I want to wiggle when the user long-presses one of them. So similar to the behavior you get when you long-press an icon in your iPad/iPhone menu.

So I have the following:

- (void)startWiggling {

    for (UIImageView *touchView in [scrollView subviews]) {

        [UIView beginAnimations:@"wiggle" context:nil];
        [UIView setAnimationDuration:0.1];
        [UIView setAnimationRepeatAutoreverses:YES];
        [UIView setAnimationRepeatCount:FLT_MAX];

        //wiggle 1 degree both sides
        touchView.transform = CGAffineTransformMakeRotation();
        touchView.transform = CGAffineTransformMakeRotation(-0.0174532925);

        [UIView commitAnimations];

    }    

}

- (void)stopWiggling {
    NSLog(@"Stop wiggling");    
}

This works fine. The issue is... How can I make it stop wiggling after the user has pressed a button? I have a button and connected it etc and it's reaching the stopWiggling method, so that's fine. But so...

  1. How do I remove the UIView animation from these UIImageViews?
  2. Can I bind this action to the user pressing the home button on their device?

回答1:

 #import <QuartzCore/QuartzCore.h>

then

 [myView.layer removeAllAnimations];

or

[self.view.layer removeAllAnimations];