學習iOS的動畫是比較有意思,動畫可以使視圖旋轉(zhuǎn),平移,放大,縮小,平移,恢復,實現(xiàn)坐標的改變和尺寸的改變,現(xiàn)在我們來看看動畫是如何定義的.
首先我們了解一下動畫的一些屬性及樣式
1.動畫的屬性:
1)開啟動畫
[UIView beginAnimations:nil context:nil];
2)動畫的持續(xù)時間1秒
[UIView setAnimationDuration:1];
3) 設置動畫延時0秒
[UIView setAnimationDelay:0];
4) 動畫的重復次數(shù):2次
[UIView setAnimationRepeatCount:2];
5)結束動畫
[UIView commitAnimations];
6)transform
CGAffineTransformTranslate:平移
CGAffineTransformScale:縮放(放大,縮小,還原)
CGAffineTransformRotate:旋轉(zhuǎn)
舉例:
//平移:改變x或y坐標即可
view.transform = CGAffineTransformTranslate(view.transform, 10, 50);
NSStringFromCGRect(view.frame));
//縮放(縮小):小于1的就是縮小,大于1便是放大
view.transform = CGAffineTransformScale(view.transform, 0.5, 0.5);
//旋轉(zhuǎn):旋轉(zhuǎn)M_PI/4
view.transform = CGAffineTransformRotate(view.transform, M_PI/4);
還原例子:
[UIView beginAnimations:@"" context:nil];? //開始動畫
[UIView setAnimationDelegate:self];
CGAffineTransform curent =? HalfTopbackgrond.transform;
CGAffineTransform scale = CGAffineTransformScale(curent, 1.2,1.2);
[UIView setAnimationDuration:5];
[HalfTopbackgrond setTransform:scale];? //再次設置transform后還原
[UIView commitAnimations];? //結束動畫
6)設置代理屬性
//可以通過代理監(jiān)測動畫的執(zhí)行狀態(tài)
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(animationStart)];
[UIView setAnimationDidStopSelector:@selector(animationStop)];
代碼如下:
//
//? AppDelegate.m
//? 07_Animation
//
//? Created by wxhl on 16/4/10.
//? Copyright ? 2016年 wxhl. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//--------------------1. 初始化_window--------------------
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor whiteColor];
[_window makeKeyAndVisible];
_window.rootViewController = [[UIViewController alloc] init];
//--------------------2. 創(chuàng)建view--------------------
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
view.backgroundColor = [UIColor brownColor];
[_window addSubview:view];
//--------------------3. UI的動畫屬性--------------------
//1) 開啟動畫
[UIView beginAnimations:nil context:nil];
//2) 設置動畫的持續(xù)時間
[UIView setAnimationDuration:1];
//3) 設置動畫延時
[UIView setAnimationDelay:0];
//4) 重復次數(shù)
[UIView setAnimationRepeatCount:2];
//5) 動畫的自動回放
[UIView setAnimationRepeatAutoreverses:YES];
//--------------------4. 屬性控制--------------------
/* 可以做動畫的屬性
frame - 改變視圖的尺寸和位置
bounds - 改變尺寸,原點坐標(慎用)
center - 中心點位置
transform - 平移、縮放、旋轉(zhuǎn)
alpha - 透明度
backgroundColor - 背景顏色
*/
//1) 修改透明度
view.alpha = 0.618;
//2) 修改frame大小
view.frame = CGRectMake(0, 300, 200, 200);
NSLog(@"1、view.frame = %@", NSStringFromCGRect(view.frame));
//3) 修改transform屬性
view.transform = CGAffineTransformTranslate(view.transform, 10, 50);? ? //平移
NSLog(@"2、view.frame = %@", NSStringFromCGRect(view.frame));
view.transform = CGAffineTransformScale(view.transform, 0.5, 0.5);? ? ? //縮放
NSLog(@"3、view.frame = %@", NSStringFromCGRect(view.frame));
view.transform = CGAffineTransformRotate(view.transform, M_PI/4);? ? ? //旋轉(zhuǎn)
view.transform = CGAffineTransformRotate(view.transform, M_PI/4);
NSLog(@"4、view.frame = %@", NSStringFromCGRect(view.frame));
//--------------------5. 設置代理屬性--------------------
//可以通過代理監(jiān)測動畫的執(zhí)行狀態(tài)
[UIView setAnimationDelegate:self];
[UIView setAnimationWillStartSelector:@selector(animationStart)];
[UIView setAnimationDidStopSelector:@selector(animationStop)];
//--------------------6. 結束動畫--------------------
[UIView commitAnimations];
return YES;
}
- (void) animationStart {
NSLog(@"動畫開始啟動了...");
}
- (void) animationStop {
NSLog(@"動畫已經(jīng)結束了...");
}
@end
運行結果:

