我需要在四个视图上应用动画。如果用户点击按钮,动画view1
应该隐藏 1 秒,然后view2
,然后view3
,然后view4
。
问题是如何在不同的视图中按顺序应用动画?
我需要使用 Core Animation 来完成它。
我需要在四个视图上应用动画。如果用户点击按钮,动画view1
应该隐藏 1 秒,然后view2
,然后view3
,然后view4
。
问题是如何在不同的视图中按顺序应用动画?
我需要使用 Core Animation 来完成它。
使用animateWithDuration:animations:completion:
完成块调用运行下一个动画的方法的方法(类似地,它的完成块开始链的下一部分)。
试试这个代码,
H。
NSInteger currentView;
UIView *view1, *view2, *view3, *view4;
米。
-(无效)viewDidLoad {
[super viewDidLoad];
currentView = 1;
view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 100)];
[view1 setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view1];
view2 = [[UIView alloc]initWithFrame:CGRectMake(80, 0, 80, 100)];
[view2 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:view2];
view3 = [[UIView alloc]initWithFrame:CGRectMake(160, 0, 80, 100)];
[view3 setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:view3];
view4 = [[UIView alloc]initWithFrame:CGRectMake(240, 0, 80, 100)];
[view4 setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:view4];
}
-(IBAction)buttonPressed:(id)sender {
[self runAnimation];
}
-(UIView *)viewForAnimation:(NSInteger)index {
switch (index)
{
case 1:
return view1;
case 2:
return view2;
case 3:
return view3;
case 4:
return view4;
}
return view1;
}
-(无效)运行动画{
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear
animations:^
{
[self viewForAnimation:currentView].hidden = YES;
}
completion:^(BOOL finished)
{
currentView++;
if(currentView < 5)
[self performSelector:@selector(runAnimation) withObject:nil afterDelay:1.5];
}
];
}
嵌套动画可以帮助你实现你想要的。
在 UIView 中引用动画
- (IBAction)yoiurButtonClickAction:(id)sender{
[UIView animateWithDuration:1.0
delay: 0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
_view1.alpha = 0.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
delay: 0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_view1.alpha = 1.0;
_view2.alpha = 0.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
delay: 0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_view2.alpha = 1.0;
_view3.alpha = 0.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
delay: 0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_view3.alpha = 1.0;
_view4.alpha = 0.0;
}
completion:^(BOOL finished){
[UIView animateWithDuration:1.0
delay: 0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
_view4.alpha = 1.0;
}
completion:^(BOOL finished){
}];
}];
}];
}];
}];
}