Sto*_*ner 5 iphone core-animation objective-c uiview ios
//卡类的头文件
#import "OWCardView.h"
@implementation OWCardView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.contents = (id) [UIImage imageNamed:@"4.png"].CGImage;
self.layer.shadowColor = [UIColor whiteColor].CGColor;
self.layer.shadowOpacity = 0.3;
}
return self;
}
- (void) moveCard:(float)xPoint along:(float)yPoint
{
CGRect someRect = CGRectMake(xPoint, yPoint, 72, 96);
[UIView animateWithDuration: 3.5
delay: 0.3
options: UIViewAnimationCurveEaseOut
animations: ^{ self.frame = someRect;}
completion: ^(BOOL finished) { }];
}
@end
// part of the Implementation in the view controller's viewDidLoad
[...]
CGRect myImageRect = CGRectMake(20.0f, 20.0f, 72.0f, 96.0f);
// myCard is a property of the viewController
self.myCard = [[OWCardView alloc] initWithFrame:myImageRect];
[self.view addSubview:self.myCard];
[self.myCard moveCard: 240 along: 355]; // First call
[self.myCard moveCard: 50 along: 355]; // Second call
[self.myCard moveCard: 50 along: 50]; // Third call
[...]
Run Code Online (Sandbox Code Playgroud)
问题:只执行viewController中moveCard的最后一条消息; 第一次和第二次调用不执行.代码有什么问题?
当我将第三条消息注释到moveCard时,第二个调用被执行而第一个调用没有.如果我评论第二个和第三个呼叫,则执行第一个呼叫.但我希望所有三个动画都出现在卡片上.
您遇到的问题是动画是异步的。
该代码不会等待每个动画完成后再执行下一个动画。因此,它会爆炸前两个调用并立即启动第三个动画,并取消前两个动画,这意味着您只会看到最后一个动画。
动画完成后,您必须使用已完成的块来执行某些操作。
做到这一点的简单方法就像这样......
[UIView animateWithDuration: 3.5
delay: 0.3
options: UIViewAnimationCurveEaseOut
animations: ^{ self.frame = CGRectMake(240, 355, 72, 96);}
completion: ^(BOOL finished) {
[UIView animateWithDuration: 3.5
delay: 0.3
options: UIViewAnimationCurveEaseOut
animations: ^{ self.frame = CGRectMake(50, 355, 72, 96);}
completion: ^(BOOL finished) {
[UIView animateWithDuration: 3.5
delay: 0.3
options: UIViewAnimationCurveEaseOut
animations: ^{ self.frame = CGRectMake(50, 50, 72, 96);}
completion: ^(BOOL finished) {
//All finished
}];
}];
}];
Run Code Online (Sandbox Code Playgroud)
您可以将其放入某种递归函数中,但这样做可能更容易。
另一种选择是使用关键帧动画,您可以逐步建立路径和计时,然后说“GO”,它就会执行您建立的动画。
不过现在这可能更容易,而且我以前用过这种东西。
| 归档时间: |
|
| 查看次数: |
164 次 |
| 最近记录: |