简体   繁体   中英

UIView animation on completion not working properly

I have a problem using a slider to trigger a series of animations, here's the code:

-(void)slideAlpha:(id)sender{

  self.bigPhotoViewA.alpha = self.alphaSlider.value;

  if (self.alphaSlider.value == 1){

    [UIView animateWithDuration:1
                     animations:^{
                         self.alphaSlider.alpha = 0;
                     } completion:nil
    ];

    [self performSelector:@selector(nextPhotoAnimation) withObject:self afterDelay:5.0 ];
  }
}

-(void) nextPhotoAnimation{

  self.alphaSlider.value = 0;

  [UIView animateWithDuration:2
                   animations:^{
                       self.bigPhotoViewA.alpha = 0.0;
                       self.bigPhotoView.alpha = 0.0;
                       self.smallPhotoView.center = CGPointMake(startX, startY);
                   }
                   completion:^(BOOL finished) {
                       NSLog(@"Animation ended");
                       self.smallPhotoView.image = ((UIImage *)[smallImagesArray objectAtIndex:imageCount]);
                   }
  ];
}

So, when the slider reaches a value of 1, nextPhotoAnimation is launched after a delay. So far so good. The problem comes inside nextPhotoAnimation . The animations block runs ok, but the completion block runs several times every time nextPhotoAnimation is called. I get the NSLog from 6 to 9 times displayed when nextPhotoAnimation starts, and then I get it again at the right time, after 2 seconds.

的NSLog

I've tried to replicate the problem with simpler code and the animation / completion flow works just fine.

Try this in nextPhotoAnimation ,

-(void) nextPhotoAnimation{

    self.alphaSlider.value = 0;

    [UIView animateWithDuration:2
               animations:^{
                   self.bigPhotoViewA.alpha = 0.0;
                   self.bigPhotoView.alpha = 0.0;
                   self.smallPhotoView.center = CGPointMake(startX, startY);
               }
               completion:^(BOOL finished) {
                   if (finished) {
                       NSLog(@"Animation ended");
                       self.smallPhotoView.image = ((UIImage *)[smallImagesArray objectAtIndex:imageCount]);
                   }
               }
    ];
}

neither you are using

[UIView beginAnimations:nil context:nil];

nor

[UIView commitAnimations]; Best way is to create a UIView and then use the protocols.call your methods using [self YOURMETHOD]; in the View. :-)

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