我有一系列8个UIView动画,在我的视图加载后直接出现。现在,我正在使用animationDidStop:finished:context
委托方法完成此任务,一切都按预期工作。问题是我为每个动画都有一个新方法。重复每个方法中的大多数代码,只有动画持续时间和元素的实际定位发生变化。
我尝试创建一个可以调用的方法,并让上下文保存适当更改UI所需的参数,但它似乎递归调用自己超过了我调用它的次数:
-(void)animationDidStop:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
NSNumber *number = (NSNumber *)context;
int animationStep = [number intValue];
int nextAnimationStep = animationStep + 1;
NSNumber *nextAnimationStepNumber = [NSNumber numberWithInt:nextAnimationStep];
NSLog(@"Animation Step: %i", animationStep);
CGRect firstFrame = CGRectMake(self.feedsScroll.frame.size.width * 2, 0.0f, self.secondFeedView.view.frame.size.width, self.secondFeedView.view.frame.size.height);
CGRect thirdFrame = CGRectMake(self.feedsScroll.frame.size.width * 2, 0.0f, self.thirdFeedView.view.frame.size.width, self.thirdFeedView.view.frame.size.height);
[UIView beginAnimations:nil context:nextAnimationStepNumber];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDelegate:self];
if (animationStep < 8)
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
NSLog(@"Beginning animations");
switch (animationStep) {
case 0:
[UIView setAnimationDuration:.3];
self.firstFeedView.view.center = CGPointMake(self.firstFeedView.view.center.x + 30, self.firstFeedView.view.center.y);
break;
case 1:
[UIView setAnimationDuration:.3];
self.firstFeedView.view.center = CGPointMake(self.firstFeedView.view.center.x - 30, self.firstFeedView.view.center.y);
break;
case 2:
[UIView setAnimationDuration:.3];
[self.secondFeedView.view setFrame:firstFrame];
break;
case 3:
[UIView setAnimationDuration:.3];
self.secondFeedView.view.center = CGPointMake(self.secondFeedView.view.center.x + 30, self.firstFeedView.view.center.y);
break;
case 4:
[UIView setAnimationDuration:.3];
self.secondFeedView.view.center = CGPointMake(self.secondFeedView.view.center.x - 30, self.firstFeedView.view.center.y);
break;
case 5:
NSLog(@"Animation step 6");
[UIView setAnimationDuration:.5];
self.firstFeedView.view.center = CGPointMake(self.firstFeedView.view.center.x - 230, self.firstFeedView.view.center.y);
self.secondFeedView.view.center = CGPointMake(self.secondFeedView.view.center.x - 230, self.firstFeedView.view.center.y);
break;
case 6:
[UIView setAnimationDuration:.5];
[self.thirdFeedView.view setFrame:thirdFrame];
break;
case 7:
[UIView setAnimationDuration:.3];
self.thirdFeedView.view.center = CGPointMake(self.thirdFeedView.view.center.x + 30, self.firstFeedView.view.center.y);
break;
case 8:
[UIView setAnimationDuration:.3];
self.thirdFeedView.view.center = CGPointMake(self.thirdFeedView.view.center.x - 30, self.thirdFeedView.view.center.y);
break;
default:
break;
}
[UIView commitAnimations];
}
我知道这可能是一个天真的实现。我是iPhone开发的新手,我正在寻找一些适用于此的最佳实践。我是以错误的方式解决这个问题吗?
答案 0 :(得分:19)
如果你愿意升级到iOS 4.0,那么新的基于块的动画方法可以使这个动画变得微不足道。例如:
[UIView animateWithDuration:1.0 animations:^{ view.position = CGPointMake(0.0f, 0.0f); } completion:^(BOOL finished){
[UIView animateWithDuration:0.2 animations:^{ view.alpha = 0.0; } completion:^(BOOL finished){
[view removeFromSuperview]; }];
}]
将使view
在1秒的持续时间内动画显示为(0,0),淡出0.2秒,然后从其超级视图中删除。这些动画将是连续的。
+animateWithDuration:animations:completion:
类方法中的第一个块包含一次性动画的属性更改,第二个块是动画完成时要执行的操作。因为此回调可以包含此类动画,所以您可以将它们嵌套以创建任意动画链。
答案 1 :(得分:16)
您可以使用块来获得非常干净的结果。
NSMutableArray* animationBlocks = [NSMutableArray new];
typedef void(^animationBlock)(BOOL);
// getNextAnimation
// removes the first block in the queue and returns it
animationBlock (^getNextAnimation)() = ^{
animationBlock block = (animationBlock)[animationBlocks firstObject];
if (block){
[animationBlocks removeObjectAtIndex:0];
return block;
}else{
return ^(BOOL finished){};
}
};
//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
[UIView animateWithDuration:1.0 animations:^{
//...animation code...
} completion: getNextAnimation()];
}];
//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
[UIView animateWithDuration:1.0 animations:^{
//...animation code...
} completion: getNextAnimation()];
}];
//add a block to our queue
[animationBlocks addObject:^(BOOL finished){;
NSLog(@"Multi-step Animation Complete!");
}];
// execute the first block in the queue
getNextAnimation()(YES);
取自:http://xibxor.com/objective-c/uiview-animation-without-nested-hell/
答案 2 :(得分:10)
我们使用块(CPAnimationSequence on Github)以声明方式创建了一个用于链接动画步骤的组件。我们描述了动机和理由in our iOS development blog。
它为您提供了非常易读的代码,如下所示:
[[CPAnimationSequence sequenceWithSteps:
[CPAnimationStep for:0.25 animate:^{ self.imageView.alpha = 0.0; }],
[CPAnimationStep for:0.25 animate:^{ self.headline.alpha = 0.0; }],
[CPAnimationStep for:0.25 animate:^{ self.content.alpha = 0.0; }],
[CPAnimationStep after:1.0 for:0.25 animate:^{ self.headline.alpha = 1.0; }],
[CPAnimationStep for:0.25 animate:^{ self.content.alpha = 1.0; }],
nil]
runAnimated:YES];
答案 3 :(得分:1)
“上下文”是一个空*,因此不予保留。有几个选择:
id
)。int animationStep = [animationID intValue];
和[UIView beginAnimations:[NSString stringWithFormat:@"%d", nextAnimationStep] context:NULL];
。这也感觉很蠢。int animationStep = (int)context;
和[UIView beginAnimations:nil context:(void*)nextAnimationStep];
。这很蹩脚(根据C标准可能导致未定义的行为)。另一方面,finished:(BOOL)finished
应为finished:(NSNumber*)finished
。他们在原始文档中犯了一个错误;它可能更容易更改文档以反映API而不是更改API并且必须神奇地保持向后兼容性(即使传递bool更合理)。
答案 4 :(得分:-1)
// create the view that will execute our animation
UIImageView* logo = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
logo.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"frame_00001.png"],
[UIImage imageNamed:@"frame_00002.png"],
[UIImage imageNamed:@"frame_00003.png"],
[UIImage imageNamed:@"frame_00004.png"],
[UIImage imageNamed:@"frame_00005"], nil];
// all frames will execute in 3 seconds
logo.animationDuration =3.3;
// repeat the annimation forever
logo.animationRepeatCount = 1;
// start animating
[logo startAnimating];
// add the animation view to the main window
[self.view addSubview:logo];
[logo release];
将其放入ViewDidLoad