UIViewAnimation, 是UIView自帶的最基礎(chǔ)的動畫, 使用方便, 但是效果有限.
使用實例:
#import "ViewController.h"
@interface ViewController ()
//聲明一個控件屬性
@property (strong, nonatomic) UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#1. 初始化該控件
self.label = [[UILabel alloc] init];
#2. 設(shè)置控件的位置大小
self.label.bounds = CGRectMake(0, 0, 100, 100);
#3. 錨點(默認位置為(0.5, 0.5), 即控件的中心點, 相對于自身來說), 控件錨點的位置, 與父視圖的(0, 0)點重合
self.label.layer.anchorPoint = CGPointMake(0, 0);
#4. 設(shè)置背景顏色, 并添加到視圖上
self.label.backgroundColor = [UIColor redColor];
[self.view addSubview:self.label];
# 以上控件的設(shè)置, 是為了在動畫執(zhí)行時能夠明顯的表示出來
******************************************************************
# 動畫開始執(zhí)行時觸發(fā)的方法, 同下面的開始方法
[UIView setAnimationWillStartSelector:@selector(startAC:)];
}
# 動畫開始執(zhí)行時觸發(fā)的方法
- (void)animationWillStart:(NSString *)animationID context:(void *)context{
NSLog(@"開始*********");
}
#通過Touch手勢, 觸發(fā)動畫
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1. 創(chuàng)建一個動畫
[UIView beginAnimations:nil context:nil];
//2. 動畫延遲設(shè)置
[UIView setAnimationDelay:1];
//3. 給動畫添加代理(不遵循代理協(xié)議, 也能實現(xiàn)代理方法)
[UIView setAnimationDelegate:self];
//4. 給動畫添加方法(動畫結(jié)束后執(zhí)行)
[UIView setAnimationDidStopSelector:@selector(stopAc)];
//5. 動畫持續(xù)時間(完成動畫所需時間)
[UIView setAnimationDuration:2];
//6. 設(shè)置動畫是否會自動按原路徑返回
[UIView setAnimationRepeatAutoreverses:NO];
//7. 設(shè)置動畫移動的新位置
self.label.frame = CGRectMake(100, 100, 100, 100);
//8. 開始動畫
[UIView commitAnimations];
}
- (void)stopAc{
NSLog(@"動畫結(jié)束后執(zhí)行的方法");
}
@end```
>#*UIViewAnimation常用的方法
# 創(chuàng)建一個動畫, 傳遞上下文等信息
+ (void)beginAnimations:(nullable NSString *)animationID context:(nullable void *)context;
# 設(shè)置完動畫參數(shù)后, 開始動畫
+ (void)commitAnimations;
# 設(shè)置代理, 代理可以遵循也可以不
+ (void)setAnimationDelegate:(nullable id)delegate; // default = nil
# 動畫將要開始時,觸發(fā)的方法
+ (void)setAnimationWillStartSelector:(nullable SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
# 動畫結(jié)束后執(zhí)行的方法
+ (void)setAnimationDidStopSelector:(nullable SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
# 設(shè)置動畫執(zhí)行的持續(xù)時間
+ (void)setAnimationDuration:(NSTimeInterval)duration; // default = 0.2
# 設(shè)置動畫開始執(zhí)行的延遲時間
+ (void)setAnimationDelay:(NSTimeInterval)delay; // default = 0.0
+ (void)setAnimationStartDate:(NSDate *)startDate; // default = now ([NSDate date])
# 設(shè)置動畫塊中的動畫屬性變化的曲線 ,動畫進入/退出視圖的效果
+ (void)setAnimationCurve:(UIViewAnimationCurve)curve; // default = UIViewAnimationCurveEaseInOut
# 設(shè)置動畫執(zhí)行的次數(shù)
+ (void)setAnimationRepeatCount:(float)repeatCount; // default = 0.0. May be fractional
# 設(shè)置動畫是否會"按原動畫路徑執(zhí)行返回的動畫"
+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses; // default = NO. used if repeat count is non-zero
# 設(shè)置動畫從當前狀態(tài)開始播放
+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState; // default = NO. If YES, the current view position is always used for new animations -- allowing animations to "pile up" on each other. Otherwise, the last end state is used for the animation (the default).