calayer动画总结(一)
核心動畫提供了一套你可以在你的應用程序里面使用的動畫類的表現:
第一、CABasicAnimation提供了在圖層的屬性值間簡單的插入;
第二、CAKeryframeAnimation提供支持關鍵幀動畫。你指定動畫的一個圖層屬性的關鍵路徑,一個表示在動畫的每個階段的數組,還有一個關鍵幀時間的數組和時間的函數;
第三、CATransition提供了一個影響整個圖層的內容過渡效果。在動畫顯示過程中采用淡出(fade)、推出(push)、顯露(reveal)圖層的內容。常用的過渡效果可以通過提供你自己的定制的核心圖像濾鏡來擴展。
除了要指定顯示的動畫類型,你還必須指定動畫的間隔、它的速度(它的插值如何分布在整個動畫過程)、動畫循環時候的循環次數、動畫周期完成的時候是否自動的反轉、還有動畫結束的時候它的可視化狀態。動畫類和CAMediaTiming協議提供所有這些功能甚至更多的功能。
一、CALayer
由于核心動畫集成到了 cocoa 中,因而你通過給窗口、視圖、層簡單的設定一些你感興趣的屬性值, 從而使那些可視的組件做動畫到新的值。當你使用 CALayer 的時候,你需要做的就是直接設置這些值。例如, 如果你想改變一個層的邊框大小,你簡單的調用[layer setBounds:newFrame],這里要說的是,層是你已經創建 的 CALayer 對象,并且要增加這個對象到層樹中,而 newFrame 是一個 CGRect 包含了邊框的尺寸和原點。當 這些代碼運行時,就會使用關鍵路徑“bounds”的默認動畫,使層具有邊框大小改變時的動畫。
簡單的說,當你使用一個窗口(NSWindow)或者視圖(NSView),你需要做的就是,使用了動畫的代理對象, 來設置窗口或者視圖的屬性值。這意味著,例如,你可以代替使用[view setFrame:newFrame]來設置窗口的框架, 通過[[view animtor] setFrame:newFrame]這個函數調用,達到目的。不同之處就是,你用了一個動畫的代理對象, 設置了這些屬性,而這些都會隱式的執行框架的初始值到結束值的動畫。
?
1、
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, 320, 100)];
? ? imageView.userInteractionEnabled=YES;
? ? [self.view addSubview:imageView];
// Do any additional setup after loading the view.
? ? NSString *path=[[NSBundle mainBundle]pathForResource:@"balloon" ofType:@"jpg"];
? ? UIImage *image=[[UIImage alloc]initWithContentsOfFile:path];
? ? layer=[CALayer layer];
?? ?
? ? x=0;
? ? y=0;
? ? width=320;
? ? height=300;
?? ?
? ? [layer setFrame:CGRectMake(x, y, width, height)];
? ? // Set the layer contents to our baloon image.
? ? [layer setContents:(id)[self nsImageToCGImageRef:image]];
? ? // Add our layer to the sublayers at index 0 so that is displays
? ? // behind the controls we added
? ? [[imageView layer]insertSublayer:layer atIndex:0];
? ? [image release];
二、CABasicAnimation
你已經發現了,所有可以在層上做動畫的屬性。然而,在動畫對象(CABasicAnimation)中還有許多 有用的屬性,這些屬性可以讓你便于控制動畫,提升動畫的性能。
Autoreverses
當你設定這個屬性為 YES 時,在它到達目的地之后,動畫的屬性會返回到開始的值,代替了直接跳轉到 開始的值。
DurationDuration 這個參數你已經相當熟悉了。它設定開始值到結束值花費的時間。期間會被速度的屬性所影響。 RemovedOnCompletion這個屬性默認為 YES,那意味著,在指定的時間段完成后,動畫就自動的從層上移除了。這個一般不用。
假如你想要再次用這個動畫時,你需要設定這個屬性為 NO。這樣的話,下次你在通過-set 方法設定動畫的屬 性時,它將再次使用你的動畫,而非默認的動畫。
Speed
默認的值為 1.0.這意味著動畫播放按照默認的速度。如果你改變這個值為 2.0,動畫會用 2 倍的速度播放。 這樣的影響就是使持續時間減半。如果你指定的持續時間為 6 秒,速度為 2.0,動畫就會播放 3 秒鐘---一半的 持續時間。
BeginTime
這個屬性在組動畫中很有用。它根據父動畫組的持續時間,指定了開始播放動畫的時間。默認的是 0.0.組 動畫在下個段落中討論“Animation Grouping”。
TimeOffset
如果一個時間偏移量是被設定,動畫不會真正的可見,直到根據父動畫組中的執行時間得到的時間都流逝 了。
RepeatCount
默認的是 0,意味著動畫只會播放一次。如果指定一個無限大的重復次數,使用 1e100f。這個不應該和 repeatDration 屬性一塊使用。
RepeatDuration
這個屬性指定了動畫應該被重復多久。動畫會一直重復,直到設定的時間流逝完。它不應該和 repeatCount 一起使用。
?
2、
CGPoint startPoint=CGPointMake(280, 300);
? ? CGPoint endPoint=CGPointMake(30, 50);
? CABasicAnimation *animation =
? ? [CABasicAnimation animationWithKeyPath:@"position"];
? ? ? ? [animation setFromValue:[NSValue valueWithCGPoint:startPoint]];
? ? ? ? [animation setToValue:[NSValue valueWithCGPoint:endPoint]];
? ? ? ? [animation setDuration:5.0];
? ? ? ? [animation setFillMode:kCAFillModeForwards];
? ? ? ? [[button1 layer] setPosition:endPoint]; ? ? ? ?
? ? ? ? [[button1 layer]addAnimation:animation forKey:@"button"];
3、CAAnimationGroup
“有用的動畫屬性”,我們定義了 2 個特殊的屬性,是關系到動畫組的:beginTime 和
timeOffset。
CGRect oldRect=CGRectMake(0, 0, 100, 100);
? ? CGRect newRect=CGRectMake(0, 0, 300, 300);
?? ?
? ? CABasicAnimation *boundsAnimation=[CABasicAnimation animationWithKeyPath:@"bounds"];
? ? [boundsAnimation setFromValue:[NSValue valueWithCGRect:oldRect]];
? ? [boundsAnimation setToValue:[NSValue valueWithCGRect:newRect]];
? ? [boundsAnimation setDuration:15.0f];
? ? [boundsAnimation setBeginTime:0.0f];
?? ?
? ? CABasicAnimation *positionAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
? ? [positionAnimation setFromValue:[NSValue valueWithCGRect:[view2 layer].frame]];
? ? [positionAnimation setToValue:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
? ? [positionAnimation setDuration:15.0f];
? ? [positionAnimation setBeginTime:5.0f];
?? ?
? ? CABasicAnimation *borderWidthAnimation =
? ? [CABasicAnimation animationWithKeyPath:@"borderWidth"];
? ? [borderWidthAnimation setFromValue:[NSNumber numberWithFloat:5.0f]];
? ? [borderWidthAnimation setToValue:[NSNumber numberWithFloat:30.0f]];
? ? [borderWidthAnimation setDuration:15.0f];
? ? [borderWidthAnimation setBeginTime:10.0f];
?? ?
? ? CAAnimationGroup *group=[CAAnimationGroup animation];
? ? [group setDuration:15];
? ? [group setAnimations:[NSArray arrayWithObjects:boundsAnimation,positionAnimation,borderWidthAnimation, nil]];
? ? [[view2 layer]addAnimation:group forKey:nil];
? ?
總結
以上是生活随笔為你收集整理的calayer动画总结(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何让网页背景图片完整显示
- 下一篇: 转:lightGBM的黑科技--plot