Mar*_*ski 15 iphone core-animation uiview
我有一系列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];
}
Run Code Online (Sandbox Code Playgroud)
我知道这可能是一个天真的实现.我是iPhone开发的新手,我正在寻找一些适用于此的最佳实践.我是以错误的方式解决这个问题吗?
Bra*_*son 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]; }];
}]
Run Code Online (Sandbox Code Playgroud)
将导致view在1秒的持续时间内动画为(0,0),淡出0.2秒,然后从其超视图中移除.这些动画将是连续的.
+animateWithDuration:animations:completion:类方法中的第一个块包含一次性动画的属性更改,第二个块是动画完成时要执行的操作.因为此回调可以包含此类的另一个动画,所以可以嵌套它们以创建任意动画链.
And*_*chs 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);
Run Code Online (Sandbox Code Playgroud)
取自:http://xibxor.com/objective-c/uiview-animation-without-nested-hell/
Yan*_*yer 10
我们使用块(Github上的CPAnimationSequence)以声明方式创建了一个用于链接动画步骤的组件.我们在iOS开发博客中描述了动机和理由.
它为您提供了非常易读的代码,如下所示:
[[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];
Run Code Online (Sandbox Code Playgroud)
Bob*_*j-C -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];
Run Code Online (Sandbox Code Playgroud)
将其放入 ViewDidLoad 中
| 归档时间: |
|
| 查看次数: |
11478 次 |
| 最近记录: |