Iphone 如何在同一UIImageView上执行一行中的多个动画
我想做两个动画,一个接一个:Iphone 如何在同一UIImageView上执行一行中的多个动画,iphone,animation,uiimageview,Iphone,Animation,Uiimageview,我想做两个动画,一个接一个: [UIView beginAnimations:@"growImage" context:nil]; [UIView setAnimationDuration:0.2f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; [UIView setAnimationDelegate:self]; image6.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView beginAnimations:@"growImage" context:nil];
[UIView setAnimationDuration:0.2f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
image6.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView commitAnimations];
[UIView beginAnimations:@"shrinkImage" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDelegate:self];
image6.transform = CGAffineTransformMakeScale(0.1, 0.1);
[UIView commitAnimations];
我知道我可以做第二个动画
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
或者通过设置
[UIView setAnimationDidStopSelector:@selector(shrinkImage)];
但是有没有一种方法可以传递该值(在本例中是名为image6的UIIMageView),以便第二个动画与第一个动画发生在同一对象上
谢谢 这是我的代码中的一个示例。但你明白了。您需要使用CGAffineTransformConcat将多个动画缝合在一起。你可以这样做-
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
angle = 0;
scaleFactor = 1;
startPoint.x = 60.0;
startPoint.y = 60.0;
CGAffineTransform scaleTrans = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(angle * M_PI/180);
boxView.center = startPoint;
boxView.transform = CGAffineTransformConcat(scaleTrans, rotateTrans);
[UIView commitAnimations];
iOS4提供了这样一个功能,您可以使用它来代替两块动画。苹果公司的文档表明,单块动画更平滑,加上易于一个接一个地缝合多个动画
更新:仔细考虑后,您可以这样做-
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{ /* first level of animation */
}
completion:^(BOOL finished){
}
];
如您所见,动画完成后,可以启动另一个动画,也可以进行清理。此基于块的动画用于将动画链接在一起…如果要使用基于选择器的方法,则在
setAnimationDidStopSelector
中指定的选择器将获得一个上下文
参数,该参数是在beginAnimations:context:
调用中指定的
因此,如果您将beginAnimations更改为:
[UIView beginAnimations:@"growImage" context:image6];
然后,image6
将作为上下文参数传递给选择器
更简单的方法是使用块来完成相同的任务:
[UIView animateWithDuration:0.2 animations:^() {
image6.transform = CGAffineTransformMakeScale(0.9, 0.9);
}
completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^() {
image6.transform = CGAffineTransformMakeScale(0.1, 0.1);
}];
}];
谢谢你的评论。看起来您正在同时缩放和旋转。我希望增加图像,然后当动画完成后,我想缩小图像-我不能像缩放和旋转一样同时增加和缩小…我喜欢这种基于块的动画的想法,但是在使用您的代码时,我遇到了一个错误-不兼容的块指针类型初始化“void(^)(”,预期为“void(^)(void)”。注意:我是徒手键入的。可能有打字错误!请参考苹果文档或其他在线文档。。。很高兴我能帮上忙。谢谢-我将正确答案授予@highlycaffined,因为他教我如何使用上下文将值传递给animationDidStop。他还像你一样建议使用基于块的动画。我将要使用这些,我很兴奋它的存在,尽管我需要学习更多关于正确代码的知识来让它工作-谢谢!需要记住的一点是,我刚刚在网上找到的——这只在iOS4中可用,因此如果有人运行的是3.2版本的iPad或iOS 4之前的iPhone,这可能会使应用程序崩溃。太不幸了,太好了,谢谢你!我还必须将-(void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context{更改为-(void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(UIImageView*)context{