一、首尾式動畫,漸變動畫
// 1. 準備開始一個動畫
[UIView beginAnimations:nil context:nil];
// 2. 修改控件屬性的代碼放在中間
// 3. 提交動畫
[UIView commitAnimations];
// 4. 設置動畫執行的時長
[UIView setAnimationDuration:1.0];
二、序列幀動畫
//1.把所有需要的圖片加載進一個臨時數組
// imageNamed:這個方法初始化的圖片會加載到內存到,并且一直不會釋放掉
// imageWithContentsOfFile:這個方法初始化圖片, 是從磁盤中加載圖片.
// 2. 把這些圖片一一播放
// 2.1 把要播放動畫的圖片全部放到animationImages
self.tom.animationImages = tmpImages;
// 設置動畫播放次數
self.tom.animationRepeatCount = 1;
// 播放的時長
self.tom.animationDuration = count * 0.05f;
// 2.2 開始動畫
[self.tom startAnimating];
//3.動畫播放結束以后 清空指向數組
self.animationImages = nil;
[self setAnimationImages:nil];
[self performSelector:@selector(setAnimationImages:) withObject:nil];
/**
* ?創建關鍵幀方法
*
*? @param duration? 動畫時長
*? @param delay? ? ? 動畫延遲
*? @param options? ? 動畫效果選項
*? @param animations 動畫執行代碼
*? @param completion 動畫結束執行代碼
*/
+ (void)animateKeyframesWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewKeyframeAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
/**
*? 添加關鍵幀
*
*? @param frameStartTime 動畫相對開始時間
*? @param frameDuration? 動畫相對持續時間
*? @param animations? ? 動畫執行代碼
*/
+ (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
relativeDuration:(double)frameDuration
animations:(void (^)(void))animations;
三、塊代碼動畫
方法一:
[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:
[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:初始的速度,數值越大一開始移動越快。值得注意的是,初始速度取值較高而時間較短時,也會出現反彈情況。
/**
UIViewAnimationOptionCurveEaseInOut? ? ? ? 動畫剛開始和要結束的時候,是緩慢的
UIViewAnimationOptionCurveEaseIn? ? ? ? ? ? 動畫剛開始,是緩慢的
UIViewAnimationOptionCurveEaseOut? ? ? ? ? 動畫要結束的時候,是緩慢的
UIViewAnimationOptionCurveLinear? ? ? ? ? 勻速
*/
---------------------------------------------
代碼示例:https://github.com/tengcoding/TTAnimationDemo