標籤:
相關資料:
這個理論比較多:http://www.360doc.com/content/15/0727/09/20918780_487655250.shtml
這個實踐比較多,常見的效果都有了http://www.cnblogs.com/wengzilin/p/4250957.html
例子:放大效果。思路是讓CALayer動,CABasicAnimation是怎麼動,然後將動畫加到CALayer
//演員初始化 CALayer *scaleLayer = [[CALayer alloc] init]; scaleLayer.backgroundColor = [UIColor blueColor].CGColor; scaleLayer.frame = CGRectMake(100, 100, 50, 50); scaleLayer.cornerRadius = 10; [self.view.layer addSublayer:scaleLayer]; //設定劇本 CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0]; scaleAnimation.toValue = [NSNumber numberWithFloat:1.5]; scaleAnimation.autoreverses = YES; scaleAnimation.fillMode = kCAFillModeForwards; scaleAnimation.repeatCount = MAXFLOAT; scaleAnimation.duration = 0.8; //開演 [scaleLayer addAnimation:scaleAnimation forKey:@"scaleAnimation"];
思考:這邊要建立一個CALayer,但是如果我想讓介面上某個UIView動起來(可能是一個按鈕、圖片,等)
其實所有的視圖都繼承UIView,每個UIView都有CALayer的屬性(見第一個參考資料的描述)
所以,我們可以直接將動畫加在view的layer上。這樣代碼就變得更易懂了。
下面是個視圖旋轉的例子,麼麼噠
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(200, 100, 50, 50)]; v.backgroundColor = [UIColor redColor]; [self.view addSubview:v]; CABasicAnimation *an2 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; an2.fromValue = 0; an2.toValue = [NSNumber numberWithFloat:M_PI*2]; an2.repeatCount = MAXFLOAT; an2.duration = 0.8; [v.layer addAnimation:an2 forKey:@"caan"];
iOS 核心動畫 Core Animation