- (void)leftClick {
[UIView beginAnimations:nil context:nil];
//display mode, slow at beginning and end
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//動畫時間
[UIView setAnimationDuration:1.0f];
//使用當前正在運行的狀態開始下一段動畫
[UIView setAnimationBeginsFromCurrentState:YES];
//給視圖添加過渡效果
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:imageView cache:YES];
[UIView commitAnimations];
}
- (void)rightClick {
[UIView beginAnimations:nil context:nil];
//display mode, slow at beginning and end
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//動畫時間
[UIView setAnimationDuration:1.0f];
//使用當前正在運行的狀態開始下一段動畫
[UIView setAnimationBeginsFromCurrentState:YES];
//給視圖添加過渡效果
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:imageView cache:YES];
[UIView commitAnimations];
}
- (void)upClick {
[UIView beginAnimations:nil context:nil];
//display mode, slow at beginning and end
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//動畫時間
[UIView setAnimationDuration:1.0f];
//使用當前正在運行的狀態開始下一段動畫
[UIView setAnimationBeginsFromCurrentState:YES];
//給視圖添加過渡效果
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:imageView cache:YES];
[UIView commitAnimations];
}
- (void)downClick {
[UIView beginAnimations:nil context:nil];
//display mode, slow at beginning and end
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//動畫時間
[UIView setAnimationDuration:1.0f];
//使用當前正在運行的狀態開始下一段動畫
[UIView setAnimationBeginsFromCurrentState:YES];
//給視圖添加過渡效果
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:imageView cache:YES];
[UIView commitAnimations];
}
/*
CATransition的type屬性
1.#define定義的常量
kCATransitionFade 交叉淡化過渡
kCATransitionMoveIn 新視圖移到舊視圖上面
kCATransitionPush 新視圖把舊視圖推出去
kCATransitionReveal 將舊視圖移開,顯示下面的新視圖
2.用字符串表示
pageCurl 向上翻一頁
pageUnCurl 向下翻一頁
rippleEffect 滴水效果
suckEffect 收縮效果,如一塊布被抽走
cube 立方體效果
oglFlip 上下翻轉效果
*/
- (void)MyCAnimation1 {
CATransition *animation = [CATransition animation];
//動畫時間
animation.duration = 1.0f;
//display mode, slow at beginning and end
animation.timingFunction = UIViewAnimationCurveEaseInOut;
//過渡效果
animation.type = kCATransitionMoveIn;
//過渡方向
animation.subtype = kCATransitionFromTop;
//添加動畫
[imageView.layer addAnimation:animation forKey:nil];
}
- (void)MyCAnimation2 {
CATransition *animation = [CATransition animation];
//動畫時間
animation.duration = 1.0f;
//display mode, slow at beginning and end
animation.timingFunction = UIViewAnimationCurveEaseInOut;
//在動畫執行完時是否被移除
animation.removedOnCompletion = NO;
//過渡效果
animation.type = @"pageCurl";
//過渡方向
animation.subtype = kCATransitionFromRight;
//暫時不知,感覺與Progress一起用的,如果不加,Progress好像沒有效果
animation.fillMode = kCAFillModeForwards;
//動畫停止(在整體動畫的百分比).
animation.endProgress = 0.7;
[imageView.layer addAnimation:animation forKey:nil];
}
- (void)MyCAnimation3 {
CATransition *animation = [CATransition animation];
//動畫時間
animation.duration = 1.0f;
//display mode, slow at beginning and end
animation.timingFunction = UIViewAnimationCurveEaseInOut;
//過渡效果
animation.type = @"pageUnCurl";
//過渡方向
animation.subtype = kCATransitionFromRight;
//暫時不知,感覺與Progress一起用的,如果不加,Progress好像沒有效果
animation.fillMode = kCAFillModeBackwards;
//動畫開始(在整體動畫的百分比).
animation.startProgress = 0.3;
[imageView.layer addAnimation:animation forKey:nil];
}
- (void)MyCAnimation4 {
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(updateButterfly) userInfo:nil repeats:YES];
}
- (void)updateButterfly {
butterflyView.animationDuration = 0.75f;
[self.view addSubview:butterflyView];
[butterflyView startAnimating];
butterflyView.center = [butterflyView randomCenterInView:self.view withInset:10.0f];
}
CATransition比較強大,一般可以使用CATransition模擬UIView的動畫。
/* 過渡效果
fade //交叉淡化過渡(不支持過渡方向)
push //新視圖把舊視圖推出去
moveIn //新視圖移到舊視圖上面
reveal //將舊視圖移開,顯示下面的新視圖
cube //立方體翻滾效果
oglFlip //上下左右翻轉效果
suckEffect //收縮效果,如一塊布被抽走(不支持過渡方向)
rippleEffect //滴水效果(不支持過渡方向)
pageCurl //向上翻頁效果
pageUnCurl //向下翻頁效果
cameraIrisHollowOpen //相機鏡頭打開效果(不支持過渡方向)
cameraIrisHollowClose //相機鏡頭關上效果(不支持過渡方向)
*/
/* 過渡方向
fromRight;
fromLeft;
fromTop;
fromBottom;
*/
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.5f; //動畫時長
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
animation.type = @"cube"; //過度效果
animation.subtype = @"formLeft"; //過渡方向
animation.startProgress = 0.0 //動畫開始起點(在整體動畫的百分比)
animation.endProgress = 1.0; //動畫停止終點(在整體動畫的百分比)
animation.removedOnCompletion = NO;
[self.view.layer addAnimation:animation forKey:@"animation"];