Kev*_*_TA 12 iphone animation objective-c uiview uiimageview
我有一个图像数组加载到UIImageView中,我通过一个循环动画.显示图像后,我想@selector调用一个以关闭当前视图控制器.使用以下代码可以很好地动画图像:
NSArray * imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"HowTo1.png"],
[UIImage imageNamed:@"HowTo2.png"],
nil];
UIImageView * instructions = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
instructions.animationImages = imageArray;
[imageArray release];
instructions.animationDuration = 16.0;
instructions.animationRepeatCount = 1;
instructions.contentMode = UIViewContentModeBottomLeft;
[instructions startAnimating];
[self.view addSubview:instructions];
[instructions release];
Run Code Online (Sandbox Code Playgroud)
16秒后,我想要一个方法来调用.我查看了UIView类方法,setAnimationDidStopSelector:但我无法使用它来处理当前的动画实现.有什么建议?
谢谢.
rob*_*off 23
用途performSelector:withObject:afterDelay::
[self performSelector:@selector(animationDidFinish:) withObject:nil
afterDelay:instructions.animationDuration];
Run Code Online (Sandbox Code Playgroud)
或使用dispatch_after:
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, instructions.animationDuration * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self animationDidFinish];
});
Run Code Online (Sandbox Code Playgroud)
小智 20
你可以简单地使用这个类别UIImageView-AnimationCompletionBlock
而对于Swift版本:UIImageView-AnimationImagesCompletionBlock
看看这堂课中的自述文件..
在项目中添加UIImageView + AnimationCompletion.h和UIImageView + AnimationCompletion.m文件,然后导入UIImageView + AnimationCompletion.h文件,在这里你要使用它.
例如.
NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
for(int i = 10001 ; i<= 10010 ; i++)
[imagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
self.animatedImageView.animationImages = imagesArray;
self.animatedImageView.animationDuration = 2.0f;
self.animatedImageView.animationRepeatCount = 3;
[self.animatedImageView startAnimatingWithCompletionBlock:^(BOOL success){
NSLog(@"Animation Completed",);}];
Run Code Online (Sandbox Code Playgroud)
用UIImageView无法做到这一点.使用延迟大部分时间都可以工作,但是在动画发生在屏幕上之前调用startAnimating后可能会有一些延迟,所以它不精确.而不是使用UIImageView进行此动画尝试使用CAKeyframeAnimation,它将在动画完成时调用委托方法.这些方面的东西:
NSArray * imageArray = [[NSArray alloc] initWithObjects:
(id)[UIImage imageNamed:@"HowTo1.png"].CGImage,
(id)[UIImage imageNamed:@"HowTo2.png"].CGImage,
nil];
UIImageView * instructions = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//create animation
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
[anim setKeyPath:@"contents"];
[anim setValues:imageArray];
[anim setRepeatCount:1];
[anim setDuration:1];
anim.delegate = self; // delegate animationDidStop method will be called
CALayer *myLayer = instructions.layer;
[myLayer addAnimation:anim forKey:nil];
Run Code Online (Sandbox Code Playgroud)
然后只需实现animationDidStop委托方法
| 归档时间: |
|
| 查看次数: |
14091 次 |
| 最近记录: |