UIView*aView=[self.view viewWithTag:100];
//开始动画,第一个参数表示名字(两个动画公用一个代理时用来区分),第二个参数是上下文,在animationWillStart:context:方法中能够接收这个参数。
[UIView beginAnimations:nil context:@"ddd"];
//设置动画代理
[UIView setAnimationDelegate:self];
//动画结束后调用的方法
[UIView setAnimationDidStopSelector:@selector(finsh:)];
//设置动画时长
[UIView setAnimationDuration:1.0];
//动画次数
[UIView setAnimationRepeatCount:2];
//自动返回
[UIView setAnimationRepeatAutoreverses:YES];
//渐变效果
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//动画过程中视图的透明度,可做呼吸效果
aView.alpha=0.1;
//动画过程中修改颜色
aView.backgroundColor=[UIColor redColor];
//动画移动
aView.frame=CGRectMake(100, 150, 100, 100);
//结束动画
[UIView commitAnimations];
也可以在点击按钮触发的方法中使用block语法:
<span style="font-size:18px;">-(void)buttonBlock:(UIButton*)button{
UIView*view=[self.view viewWithTag:100];
//使用block语法 结束时回调
[UIView animateWithDuration:2.0 animations:^{
view.backgroundColor=[UIColor redColor];
view.frame=CGRectMake(100, 150, 100, 100);
}completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}</span>
UIView翻转动画:
-(void)button1:(UIButton*)btu{
//做翻转动画
UIView*view=[self.view viewWithTag:100];
[UIView beginAnimations:nil context:NULL];
//cache是生成缓存,赋值yes后以后加载更快
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:YES];
[UIView setAnimationDuration:2.0];
[UIView setAnimationRepeatCount:2];
[UIView commitAnimations];
}