溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

UIView翻轉效果如何實現(xiàn)

發(fā)布時間:2022-10-18 15:46:57 來源:億速云 閱讀:154 作者:iii 欄目:編程語言

今天小編給大家分享一下UIView翻轉效果如何實現(xiàn)的相關知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

新建一個view-based模板工程,在ViewController文件中添加下面的代碼,即可實現(xiàn)翻轉效果;
 
- (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頭文件聲明下面的動作響應函數(shù)
//在xib文件中添加一個button,其響應函數(shù)為下面的函數(shù)
//運行程序后,點擊button就看到翻轉效果
-(IBAction)ActionFanzhuan{
 
//獲取當前畫圖的設備上下文
CGContextRef context = UIGraphicsGetCurrentContext();
 
//開始準備動畫
[UIView beginAnimations:nil context:context];
 
//設置動畫曲線,翻譯不準,見蘋果官方文檔 
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
 
//設置動畫持續(xù)時間
[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];
 
//當動畫執(zhí)行結束,執(zhí)行animationFinished方法
[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
 
//提交動畫
[UIView commitAnimations];
}
 
//動畫效果執(zhí)行完畢
- (void) animationFinished: (id) sender{
NSLog(@"animationFinished !");
}
 
 
 
運行程序,點擊按鈕,就能看到動畫效果了
 
 
 
 
下面我自己在parentView上添加了兩個子視圖實現(xiàn)動畫
 
- (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里面寫實現(xiàn)動畫的代碼,但一致未實現(xiàn)動畫效果,原來在viewDidLoad里面執(zhí)行
CGContextRef context = UIGraphicsGetCurrentContext();
后context的指針為0

以上就是“UIView翻轉效果如何實現(xiàn)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI