代碼:
- (void)viewDidLoad {
[super viewDidLoad];
//需要翻轉的視圖
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 200)];
parentView.backgroundColor = [UIColor yellowColor];
parentView.tag = 1000;
[self.view addSubview:parentView];
}
//需要在h頭文件聲明下面的動作響應函數
//在xib文件中添加一個button,其響應函數為下面的函數
//運行程序后,點擊button就看到翻轉效果
-(IBAction)ActionFanzhuan{
//獲取當前畫圖的設備上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//開始準備動畫
[UIView beginAnimations:nil context:context];
//設置動畫曲線,翻譯不準,見蘋果官方文檔
[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:parentView cache:YES]; //從左向右
// [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:parentView cache:YES];//從右向左
//設置動畫委托
[UIView setAnimationDelegate:self];
//當動畫執行結束,執行animationFinished方法
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
//提交動畫
[UIView commitAnimations];
}
//動畫效果執行完畢
- (void) animationFinished: (id) sender{
NSLog(@"animationFinished !");
}