您好,登錄后才能下訂單哦!
今天小編給大家分享一下UIView翻轉效果如何實現的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
新建一個view-based模板工程,在ViewController文件中添加下面的代碼,即可實現翻轉效果; - (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 !"); } 運行程序,點擊按鈕,就能看到動畫效果了 下面我自己在parentView上添加了兩個子視圖實現動畫 - (void)viewDidLoad { [super viewDidLoad]; UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 200)]; parentView.backgroundColor = [UIColor yellowColor]; parentView.tag = 1000; UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)]; image1.backgroundColor = [UIColor redColor]; image1.tag = 1001; UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)]; image2.backgroundColor = [UIColor blueColor]; image2.tag = 1002; [parentView addSubview:image1]; [parentView addSubview:image2]; [self.view addSubview:parentView]; } -(IBAction)ActionFanzhuan{ CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; 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]; NSInteger purple = [[parentView subviews] indexOfObject:[parentView viewWithTag:1002]]; NSInteger maroon = [[parentView subviews] indexOfObject:[parentView viewWithTag:1001]]; [parentView exchangeSubviewAtIndex:purple withSubviewAtIndex:maroon]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(animationFinished:)]; [UIView commitAnimations]; } - (void) animationFinished: (id) sender{ NSLog(@"animationFinished !"); } 另外:之前在viewDidLoad里面寫實現動畫的代碼,但一致未實現動畫效果,原來在viewDidLoad里面執行 CGContextRef context = UIGraphicsGetCurrentContext(); 后context的指針為0
以上就是“UIView翻轉效果如何實現”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。