代碼:
- (void)viewDidLoad {
[super viewDidLoad];
//需要翻轉(zhuǎn)的視圖
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 200)];
parentView.backgroundColor = [UIColor yellowColor];
parentView.tag = 1000;
[self.view addSubview:parentView];
}
//需要在h頭文件聲明下面的動作響應(yīng)函數(shù)
//在xib文件中添加一個button,其響應(yīng)函數(shù)為下面的函數(shù)
//運行程序后,點擊button就看到翻轉(zhuǎn)效果
-(IBAction)ActionFanzhuan{
//獲取當(dāng)前畫圖的設(shè)備上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//開始準(zhǔn)備動畫
[UIView beginAnimations:nil context:context];
//設(shè)置動畫曲線,翻譯不準(zhǔn),見蘋果官方文檔
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//設(shè)置動畫持續(xù)時間
[UIView setAnimationDuration:1.0];
//因為沒給viewController類添加成員變量,所以用下面方法得到viewDidLoad添加的子視圖
UIView *parentView = [self.view viewWithTag:1000];
//設(shè)置動畫效果
[UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:parentView cache:YES]; //從上向下
// [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:parentView cache:YES]; //從下向上
// [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:parentView cache:YES]; //從左向右
// [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:parentView cache:YES];//從右向左
//設(shè)置動畫委托
[UIView setAnimationDelegate:self];
//當(dāng)動畫執(zhí)行結(jié)束,執(zhí)行animationFinished方法
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
//提交動畫
[UIView commitAnimations];
}
//動畫效果執(zhí)行完畢
- (void) animationFinished: (id) sender{
NSLog(@"animationFinished !");
}