1、使用UIView類實現動畫
基本寫法,代碼必須放在Begin和Commit之間:
[UIView beginAnimations:nil context:nil]; // 開始動畫
// Code...
[UIView commitAnimations]; // 提交動畫
簡單例子:數組
[UIView beginAnimations:nil context:nil]; // 開始動畫
[UIView setAnimationDuration:10.0]; // 動畫時長動畫
/**代理
[UIView commitAnimations]; // 提交動畫
同時運行多個動畫效果:code
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
[_imageView setAlpha:0.0];
[UIView commitAnimations];blog
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
CGPoint point = _imageView.center;
point.y += 150;
[_imageView setCenter:point];
[UIView commitAnimations];
以上代碼實現的動畫效果爲( 同時執行 ):ci
一、圖像向下平移150像像rem
二、設置圖像透明度爲0。animation
指定上下文:it
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:2.0];
[_imageView setAlpha:0];
[UIView commitAnimations];
UIGraphicsGetCurrentContext():獲取當前視圖的上下文io
其它方法及屬性:
如下方法及屬性不爲所有,只例舉部分(其它沒說起到的方法及屬性請自行嘗試,謝謝):
// 開始動畫
// 提交動畫
// 設置動畫曲線,默認是勻速進行:
// 設置動畫時長:
// 默認爲YES。爲NO時跳過動畫效果,直接跳到執行後的狀態。
// 設置動畫延遲執行(delay:秒爲單位):
// 動畫的重複播放次數
// 若是爲YES,逆向(相反)動畫效果,結束後返回動畫逆向前的狀態; 默認爲NO:
// 設置動畫代理:
// 動畫將要開始時執行方法××(必需要先設置動畫代理):
// 動畫已結束時執行方法××(必需要先設置動畫代理):
/**
// 刪除全部動畫層
// 注意:層指的是layout,例:[_imageView.layer removeAllAnimations];
方法一:
[UIView animateWithDuration:4.0 // 動畫時長
animations:^{
// code
}];
方法二:
[UIView animateWithDuration:4.0 // 動畫時長
animations:^{
// code...
}
completion:^(BOOL finished) {
// 動畫完成後執行
// code...
}];
方法三:
[UIView animateWithDuration:4.0 // 動畫時長
delay:2.0 // 動畫延遲
options:UIViewAnimationOptionCurveEaseIn // 動畫過渡效果
animations:^{
// code...
}
completion:^(BOOL finished) {
// 動畫完成後執行
// code...
}];
方法四,Spring Animationring Animation):
在IOS7開始,系統動畫效果普遍應用Spring Animation :
[UIView animateWithDuration:4.0 // 動畫時長
delay:0.0 // 動畫延遲
usingSpringWithDamping:1.0 // 相似彈簧振動效果 0~1
initialSpringVelocity:5.0 // 初始速度
options:UIViewAnimationOptionCurveEaseInOut // 動畫過渡效果
animations:^{
// code...
CGPoint point = _imageView.center;
point.y += 150;
[_imageView setCenter:point];
} completion:^(BOOL finished) {
// 動畫完成後執行
// code...
[_imageView setAlpha:1];
}];
usingSpringWithDamping:它的範圍爲 0.0f 到 1.0f ,數值越小「彈簧」的振動效果越明顯。
initialSpringVelocity:初始的速度,數值越大一開始移動越快。值得注意的是,初始速度取值較高而時間較短時,也會出現反彈狀況。
轉:Spring Animation 是線性動畫或 ease-out 動畫的理想替代品。因爲 iOS 自己大量使用的就是 Spring Animation,用戶已經習慣了這種動畫效果,所以使用它能使 App 讓人感受更加天然,用 Apple 的話說就是「instantly familiar」。此外,Spring Animation 不僅能對位置使用,它適用於全部可被添加動畫效果的屬性。
方法五,關鍵幀動畫:
UIView動畫已經具有高級的方法來建立動畫,並且能夠更好地理解和構建動畫。IOS7之後蘋果新加了一個animateKeyframesWithDuration的方法,咱們可使用它來建立更多更復雜更酷炫的動畫效果,而不須要去使用到核心動畫(CoreAnimatino)。
建立關鍵幀方法:
/**
/**
下面用一個簡單的示例做解答,彩虹變化視圖:
void (^keyFrameBlock)() = ^(){
// 建立顏色數組
NSArray *arrayColors = @[[UIColor orangeColor],
[UIColor yellowColor],
[UIColor greenColor],
[UIColor blueColor],
[UIColor purpleColor],
[UIColor redColor]];
NSUInteger colorCount = [arrayColors count];
// 循環添加關鍵幀
for (NSUInteger i = 0; i < colorCount; i++) {
[UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount
relativeDuration:1 / (CGFloat)colorCount
animations:^{
[_graduallyView setBackgroundColor:arrayColors[i]];
}];
}
};
[UIView animateKeyframesWithDuration:4.0
delay:0.0
options:UIViewKeyframeAnimationOptionCalculationModeCubic | UIViewAnimationOptionCurveLinear
animations:keyFrameBlock
completion:^(BOOL finished) {
// 動畫完成後執行
// code...
}];
動畫過渡效果(Options),新增瞭如下幾個:
UIViewKeyframeAnimationOptionCalculationModeLinear = 0 << 10, // default
UIViewKeyframeAnimationOptionCalculationModeDiscrete = 1 << 10,
UIViewKeyframeAnimationOptionCalculationModePaced = 2 << 10,
UIViewKeyframeAnimationOptionCalculationModeCubic = 3 << 10,
UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10