原文地址http://blog.sina.com.cn/s/blog_884e78b20100u0pp.html
第一種:
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:kDuration];//動畫時間
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];//第一個參數:動畫類型
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
[UIView setAnimationDelegate:self];
// 動畫完畢后調用某個方法
//[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
[UIView commitAnimations];
第二種:
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = kDuration;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = kCATransitionFade;//動畫類型
animation.subtype = kCATransitionFromLeft;//動畫方向
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
[[self.view layer] addAnimation:animation forKey:@"animation"];
動畫類型還有:
- 2.用字符串表示
- pageCurl 向上翻一頁
- pageUnCurl 向下翻一頁
- rippleEffect 滴水效果
- suckEffect 收縮效果,如一塊布被抽走
- cube 立方體效果
- oglFlip 上下翻轉效果
- cameraIrisHollowOpen iphone相機打開效果
- cameraIrisHollowOpen 關閉相機的效果
- */
第三種:
[UIView animateWithDuration:1 animations:^{
[[self.view.subviews objectAtIndex:0] setAlpha:1];
[[self.view.subviews objectAtIndex:1] setAlpha:0];
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
}];
還有其他類型的調用方法,這個是最簡單的,可以加上其他的參數進行一些操作。
第二個參數是一個代碼快,來執行VIEW的屬性改變,系統會將視圖從當前狀態平滑的過度到最終狀態(就是你做修改之后的狀態)。
可以被改變的屬性有:
frame
bounds
center
transform //可以實現3D/2D效果
alpha
backgroundColor
contentStretch
比如 self.view.layer.transform = CATransform3DMakeRotation(1, -1, -1, 1); 可以在代碼中將整個VIEW動畫旋轉(這里只是一個例子,有效果,但是不怎么好看)
上面是3D變化,如果是2d的,那么就應該修改view上的transform ,而不應該按照上面的代碼。