CAMdiaTimingFunction 用法

一:Animation Timing Curves

 

1:Linear Animation Timing

 

2:Ease-In Animation Timing

 

3:Ease-Out Animation Timing

 

4:Ease-In Ease-Out Animation Timing

5:Custom Animation Timing

 

 自定義動畫執行曲線設置

We create a custom timing with the initWithControlPoints:::: method on CAMediaTimingFunction like this:

C代碼  收藏代碼
  1. - (CAMediaTimingFunction *)getTimingFunction {  
  2.         CGFloat c1x = 0.5;  
  3.         CGFloat c1y = 1.0;  
  4.         CGFloat c2x = 0.5;  
  5.         CGFloat c2y = 0.0;  
  6.       return [[CAMediaTimingFunction alloc    initWithControlPoints:cx1 :cy1 :cx2 :cy2];  
  7. }  

 

寫道
Since the initial and final values are already known (recall that they are {0, 0} and {1, 1}, respectively), we have to specify only the control 
points. In our example, we set the first control point to {0.5, 1.0} and the second control point to {0.5, 0.0}.

 

Java代碼  收藏代碼
  1. -(void)setupMover {  
  2.         NSRect bounds = self.bounds;  
  3.          NSRect moverFrame =NSInsetRect(bounds, NSWidth(bounds) / 4.0f,NSHeight(bounds) / 4.0f);  
  4.         moverFrame.origin.x = 0.0f;  
  5. mover = [[NSImageView alloc] initWithFrame:moverFrame];  
  6. [mover setImageScaling:NSScaleToFit];  
  7. [mover setImage:[NSImage imageNamed:@"photo.jpg" ]];  
  8. [self addSubview:mover];  
  9. }  
  10. - (id)initWithFrame:(NSRect)frame {  
  11. self = [super initWithFrame:frame];  
  12. if (self) {  
  13. [self setupMover];  
  14. }  
  15. return self;  
  16. }  
  17.   
  18. - (BOOL)acceptsFirstResponder {  
  19. return YES;  
  20. }  
  21. - (void)keyDown:(NSEvent *)event {  
  22. [self move];  
  23. }  
  24.   
  25. - (CABasicAnimation *)moveAnimation {  
  26. if(nil == moveAnimation) {  
  27. moveAnimation = [CABasicAnimation animation];  
  28. moveAnimation.duration = 2.0f;  
  29. moveAnimation.timingFunction =  
  30. [[CAMediaTimingFunction alloc]  
  31. initWithControlPoints:0.5 :1.0 :0.5 :0.0];  
  32. }  
  33. return moveAnimation;  
  34. }  
  35. - (void)move {  
  36. NSDictionary *animations = [NSDictionary  
  37. dictionaryWithObject:[self moveAnimation]  
  38. forKey:@"frameOrigin" ];  
  39. [mover setAnimations:animations];  
  40. NSPoint origin = mover.frame.origin;  
  41. origin.x += NSWidth(mover.frame);  
  42. [mover.animator setFrameOrigin:origin];  

發佈了7 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章