制作UIImageView Flash - 动画块

时间:2015-05-06 03:21:10

标签: xcode animation uiview uiimageview

我正试图在我正在制作的小游戏中与另一个碰撞时,使UIImageView闪光几次。

我在这次交流中找到THIS回答,并且被接受为最佳的答案是:

MyImage.alpha = 1.0f;
[UIView animateWithDuration:0.1f                     //speed of flash
                      delay:0.0f 
                    options:UIViewAnimationOptionAutoreverse
                 animations:^ {
       [UIView setAnimationRepeatCount:4.0f/2.0f];   //flash few times
       MyImage.alpha = 0.0f;
} completion:^(BOOL finished) { 
       [MyImage removeFromSuperview]; 
}];    

我也喜欢在第二个回答时编辑3的代码。但是上面是我一直在使用的那个。

效果很好,但是在闪光灯发生后我的UIImageView恢复正常时遇到了麻烦......

我已经尝试在完成块中将alpha值重新设置回1.0f,甚至在方法结束时我调用以启动动画,但是无论我想要尝试什么,都会使图像在看不见之后隐藏闪光......我错过了什么?谢谢。

修改 我也试过这种方式,图像淡出然后又回来了,但它只会做一次:(我希望它能做几次:

    -(void)AnimateComboFlash{        //call when collision occurs
        for(int i = 1; i < 5; i++){
            [self AltFlash];
        }
    }

    -(void)AltFlash{
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5f];
        MyImage.alpha = 0.0f;
        [UIView commitAnimations];
        [self AltFlashBack];
    }

    -(void)AltFlashBack{
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.5f];
        MyImage.alpha = 1.0f;
        [UIView commitAnimations];
    }

2 个答案:

答案 0 :(得分:0)

MyImage.alpha = 1.0f;
[UIView animateWithDuration:0.1f                     //speed of flash
                      delay:0.0f 
                    options:UIViewAnimationOptionAutoreverse
                 animations:^ {
   [UIView setAnimationRepeatCount:4.0f/2.0f];   //flash few times
   MyImage.alpha = 0.0f;
} completion:^(BOOL finished) { 
    if (finished) {
       [MyImage removeFromSuperview]; 
    }
}];

在设置最终值之前,您需要检查动画是否已完成。

答案 1 :(得分:0)

所以经过很长一段时间阅读关于UIImageView动画的交流中的各种问题和答案,我终于找到了一个Enceradeira的答案,UIImageView对我有用,允许闪光动画发生,然后图像返回“正常”(具有完整的1.0 alpha值)。

使用他的答案和我的问题中的代码,这是我用MyImage.alpha = 1.0f; [UIView animateWithDuration:0.1f //speed of flash delay:0.0f options:UIViewAnimationOptionAutoreverse animations:^ { [UIView setAnimationRepeatCount:4.0f/2.0f]; //flash few times MyImage.alpha = 0.0f; } completion:^(BOOL finished) { //[MyImage removeFromSuperview]; //REMOVE THE REMOVE MyImage.alpha = 1.0f; //RESET ALPHA VALUE AFTER COMPLETION }]; 闪烁几次,然后返回原始状态的方法:

MemoryStream stream = (MemoryStream)bitmapImage.StreamSource;
byte[] data = stream.ToArray();

希望这有助于你们中的一些人寻找类似的答案。