我试图在imageview上的两个图像之间闪烁动画,以引起用户注意指示器以注意到更改。我希望图像在保留新图像之前只闪烁一到两次。事件的时间表如下:
由于动画的时间很短,我觉得有一种方法可以在不使用NSTimer的情况下完成此操作。我目前的尝试低于
+(void)blinkImage:(UIImageView*)view toNewImage:(UIImage*)newImage numOfTimes:(int)numOfTimes{
float waitInterval = 0.25;
float totalWaitTime = 0.0;
UIImage *oldImage = view.image;
view.image = newImage;
totalWaitTime = totalWaitTime+waitInterval;
for (int times = 0; times<numOfTimes; times++) {
[UIView beginAnimations:@"setOld" context:nil];
[UIView setAnimationDelay:totalWaitTime];
[UIView setAnimationDuration:waitInterval];
view.image = oldImage;
totalWaitTime = totalWaitTime+waitInterval;
NSLog(@"SetOld; waitTime %f", totalWaitTime);
[UIView commitAnimations];
[UIView beginAnimations:@"setNew" context:nil];
[UIView setAnimationDelay:totalWaitTime];
[UIView setAnimationDuration:waitInterval];
view.image = newImage;
totalWaitTime = totalWaitTime+waitInterval;
NSLog(@"SetNew; waitTime %f", totalWaitTime);
[UIView commitAnimations];
}
}
然而,这并没有产生预期的结果。使用上面的代码,视图只是更改为新图像,并没有做任何其他事情。
非常感谢任何帮助。提前谢谢!
答案 0 :(得分:1)
此处将这一行放在setAnimationDuration
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:yourView cache:YES];
这里有不同类型的动画.. 你也可以用另一种方式进行动画
#define kAnimationKey @"transitionViewAnimation"
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFromBottom];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[[yourView layer] addAnimation:animation forKey:kAnimationKey];
答案 1 :(得分:0)
为什么不直接使用UIImageView内置的动画?
Animating Images
animationImages (property)
highlightedAnimationImages (property)
animationDuration (property)
animationRepeatCount (property)
– startAnimating
– stopAnimating
– isAnimating
或者您可以随时设置突出显示的图像并切换而不是整个图像
- (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage