I want to flash an UIImageview on the iPad, how do I do that?

后端 未结 4 1939
自闭症患者
自闭症患者 2021-02-08 19:10

I want to let a UIImageView flash several times.

Currently I don\'t know how to do that.

Actual code:

-(void)arrowsAnimate{
    [UIVi         


        
4条回答
  •  天涯浪人
    2021-02-08 19:54

    Use

    + (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

    and pass the UIViewAnimationOptionRepeat and probably UIViewAnimationOptionAutoreverse in your options. You shouldn't need to provide a completion block and only perform the first animation.

    Edit: here is some sample code for an image that fades in and out indefinitely.

    [UIView animateWithDuration:1.0 
                          delay:0.0 
                        options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse) 
                     animations:^{
                         self.myImageView.alpha = 1.0;
                     }
                     completion:NULL];
    

    Edit 2: I see you actually need to flash it 10 times only. I wasn't able to do that with blocks actually. When the completion block executed, the animation seemed to complete instantly the remaining 9 times. I was however able to do this with just the old-style animations quite easily.

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationRepeatCount:10.0];
    [UIView setAnimationRepeatAutoreverses:YES];
    
    self.myImageView.alpha = 1.0;
    
    [UIView commitAnimations];
    

    Edit 3: I found a way to do this with blocks.

    - (void)animate
    {
        if (self.animationCount < 10)
        {
            [UIView animateWithDuration:1.0 
                             animations:^{
                                 self.myImageView.alpha = 1.0;
                             }
                             completion:^(BOOL finished){
                                 [self animateBack];
                             }];
        }
    }
    
    - (void)animateBack
    {
        [UIView animateWithDuration:1.0 
                         animations:^{
                             self.myImageView.alpha = 0.0;
                         }
                         completion:^(BOOL finished){
                             self.animationCount++;
                             [self animate];
                         }];
    }
    

提交回复
热议问题