简体   繁体   中英

UIButton Animation issue - iOS

I'm trying to work on a UIButton animation where the button moves to a point and then is set hidden to be true. However when I tried working on the following code, the button disappeared even before the animation was completed. Am I doing it correctly? Any suggestions?

[UIView animateWithDuration:0.8
                 animations:^{

                     selectedCurveIndex = 0;
                     [tradebutton moveTo:
                      CGPointMake(51,150) duration:0.8 
                                  option:curveValues[UIViewAnimationOptionCurveEaseInOut]];
                 }
                 completion:^(BOOL finished){ 

                     [tradeButton setHidden:TRUE];

                     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
                     UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ButtonView"];

                     self.modalPresentationStyle = UIModalPresentationCurrentContext;
                     [self presentModalViewController:vc animated:NO];

                 }];

You need to make sure that finished is set to YES before moving on.

Your button hides quickly because 0.8 is a quick animation duration. You will need to figure out another place to hide the button, or you can

Try this:

[UIView animateWithDuration:0.8
                 animations:^{

                     selectedCurveIndex = 0;
                     [tradebutton moveTo:
                      CGPointMake(51,150) duration:0.8 
                                  option:curveValues[UIViewAnimationOptionCurveEaseInOut]];
                 }
                 completion:^(BOOL finished){ 

                     if ( finished ) 
                     {    
                         [tradeButton performSelector:@selector(setHidden:) withObject:@"YES" afterDelay:3.0];

                         UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
                         UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ButtonView"];

                         self.modalPresentationStyle = UIModalPresentationCurrentContext;
                         [self presentModalViewController:vc animated:NO];
                     }
                 }];

The problem is that you create a second, inner animation block in the moveTo:duration:option: method, and you set all of the animatable properties in that inner block. You don't set any animatable properties in the outer block.

That means that the system immediately thinks that the outer animation has finished, and calls the completion block right away. Meanwhile, the inner animation block is still going.

Stop using moveTo:duration:option: . It saves you almost nothing, and ends up giving you trouble like this. Throw it out and try something like this instead:

[UIView animateWithDuration:0.8 animations:^{
    tradeButton.frame = (CGRect){ CGPointMake(51, 150), tradeButton.bounds.size };
} completion:^(BOOL finished) {
    tradeButton.hidden = YES;
    // etc.
}];

Note that UIViewAnimationOptionCurveEaseInEaseOut is the default for most animations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM