实现view的翻转
- (void)viewDidLoad
{
[super viewDidLoad];
//需要翻转的视图
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 200)];
parentView.backgroundColor = [UIColor yellowColor];
parentView.tag = 1000;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 30, 30);
btn.backgroundColor = [UIColor redColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(ActionFanzhuan) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:parentView];
}
//运行程序后,点击button就看到翻转效果
-(void)ActionFanzhuan{
//1 获取当前画图的设备上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2 开始准备动画
[UIView beginAnimations:nil context:context];
//3 动画属性的一些设置
// 设置动画曲线,翻译不准,见苹果官方文档
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//设置动画持续时间
[UIView setAnimationDuration:1.0];
//因为没给viewController类添加成员变量,所以用下面方法得到viewDidLoad添加的子视图
UIView *parentView = [self.view viewWithTag:1000];
//设置动画效果
// [UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:parentView cache:YES]; //从上向下
// [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:parentView cache:YES]; //从下向上
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES]; //从左向右
// [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:parentView cache:YES];//从右向左
//设置动画委托
[UIView setAnimationDelegate:self];
//当动画执行结束,执行animationFinished方法
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
//4 提交动画
[UIView commitAnimations];
}
//动画效果执行完毕
- (void) animationFinished: (id) sender{
NSLog(@"animationFinished !");
}