CABasicAnimation 动画

来源:互联网 发布:电子书数据库有哪些 编辑:程序博客网 时间:2024/05/17 06:28

CABasicAnimation用法

 

CABasicAnimation 自己只有三个property   fromValue  toValue  ByValue

当你建一个 CABasicAnimation,你需要通-setFromValue-setToValue来指定一个开始当你增加基础动画到中的,它开始运行。当用属性做画完成,例如用位置属性做,就会立刻返回到它的初始位置 

住当你做,你至少使用了 2 象。象都是本身,一个或者层继承的,和在先前的例子中你分配给层CABasicAnimation象。因给动定了最后的(目的地),但是并不意味着当画完成的,的属性就改成了最后的。当画完成,你必须显示的的属性,这样动束后,你的才能真正的到你定的属性上。

你可以简单的停止画到你束的点上,但是这仅仅是一个视觉效果。层实际仍然是一的。要真的内部的,就像才所的你必须显示的定那个属性。例如,示的定位置的属性,你需要在-setPosition方法。但是,会造成一点问题

如果你通-set个方法示的定了属性的,那么默画将被,而非之前你定的画。在表 3-9 中演示了你置位置的方法。注意到了,使用 position 经创建了基础动,但是我示的用了-setPosition方法,就覆盖了我们设定的,使我们设定的基础动画完全没用了。如果你使用了个代,你会看到然我层结束的候放到了正确的位置,但是它使用的是默 0.25 ,而非我画里定的 5  

1

2

3

4

CABasicAnimation *animation =

[CABasicAnimation animationWithKeyPath:@”position”]; [animation setFromValue:[NSValue valueWithPoint:startPoint]]; [animation setToValue:[NSValue valueWithPoint:endPoint]]; [animation setDuration:5.0];

[layer setPosition:endpoint];

[layer addAnimation:animation forKey:nil];

 因此问题出来了,你怎么能使用我们设定的画呢?看表 3-9 的最后一行,注意到 forKey:个参数是被 nil就是什么画不能覆盖默认动画的原因。如果你改最后一行[layer addAnimation:animationforKey:@"position"],画将会按照我们设定的时间工作。当需要做,使用我们给路径指定的新画。 

 

下面是一些承的游泳的属性

Autoreverses

当你个属性 YES,在它到达目的地之后,画的返回到开始的,代替了直接跳开始的

Duration
Duration 个参数你已相当熟悉了。它定开始时间。期会被速度的属性所影响。 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 一起使用。 

 

 

下面段英文摘自苹果官方文档,将的是fromValue  toValue  ByValue  怎么使用

The interpolation valuesare used as follows:

•  Both fromValue and toValue arenon-nil. Interpolatesbetween fromValue and toValue.

•  fromValue and byValue arenon-nil. Interpolatesbetween fromValue and(fromValue + byValue).

•  byValue and toValue arenon-nil. Interpolatesbetween (toValue - byValue)and toValue.

•  fromValue is non-nil. Interpolatesbetween fromValue andthe current presentation value of the property.

•  toValue is non-nil. Interpolatesbetween the current value of keyPath in the target layer’spresentation layer andtoValue.

•  byValue is non-nil. Interpolates betweenthe current value of keyPath in the target layer’spresentation layer and that value plus byValue.

•  All propertiesare nil. Interpolatesbetween the previous value of keyPath in the target layer’spresentation layer and the current value of keyPath in thetarget layer’s presentation layer.

其他的方法还是属性等都是继承而来的

我们可以通过animationWithKeyPath键值对的方式来改变动画

animationWithKeyPath的值:

transform.scale = 比例轉換

    transform.scale.x = 闊的比例轉換

    transform.scale.y = 高的比例轉換

    transform.rotation.z = 平面圖的旋轉

    opacity = 透明度

    margin

    zPosition

    backgroundColor    背景颜色

    cornerRadius    圆角

    borderWidth

    bounds

    contents

    contentsRect

    cornerRadius

    frame

    hidden

    mask

    masksToBounds

    opacity

    position

    shadowColor

    shadowOffset

    shadowOpacity

    shadowRadius

 

下面是一些例子

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

    pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

    pulse.duration = 0.5 + (rand() % 10) * 0.05;

    pulse.repeatCount = 1;

    pulse.autoreverses = YES;

    pulse.fromValue = [NSNumber numberWithFloat:.8];

    pulse.toValue = [NSNumber numberWithFloat:1.2];

    [self.ui_View.layer addAnimation:pulse forKey:nil];

 

// bounds

  

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"bounds"];

    anim.duration = 1.f;

    anim.fromValue = [NSValue valueWithCGRect:CGRectMake(0,0,10,10)];

    anim.toValue = [NSValue valueWithCGRect:CGRectMake(10,10,200,200)];

    anim.byValue  = [NSValue valueWithCGRect:self. ui_View.bounds];

//    anim.toValue = (id)[UIColor redColor].CGColor;

//    anim.fromValue =  (id)[UIColor blackColor].CGColor;

     

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    anim.repeatCount = 1;

    anim.autoreverses = YES;

     

    [ui_View.layer addAnimation:anim forKey:nil];

//cornerRadius

  

    CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];

    anim2.duration = 1.f;

    anim2.fromValue = [NSNumber numberWithFloat:0.f];

    anim2.toValue = [NSNumber numberWithFloat:20.f];

    anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    anim2.repeatCount = CGFLOAT_MAX;

    anim2.autoreverses = YES;

     

    [ui_View.layer addAnimation:anim2 forKey:@"cornerRadius"];

 

//contents

  

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"contents"];

    anim.duration = 1.f;

    anim.fromValue = (id)[UIImage imageNamed:@"1.jpg"].CGImage;

    anim.toValue = (id)[UIImage imageNamed:@"2.png"].CGImage;

//    anim.byValue  = (id)[UIImage imageNamed:@"3.png"].CGImage;

//    anim.toValue = (id)[UIColor redColor].CGColor;

//    anim.fromValue =  (id)[UIColor blackColor].CGColor;

     

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    anim.repeatCount = CGFLOAT_MAX;

    anim.autoreverses = YES;

     

    [ui_View.layer addAnimation:anim forKey:nil];

 

 

  

[ui_View.layer setShadowOffset:CGSizeMake(2,2)];

    [ui_View.layer setShadowOpacity:1];

    [ui_View.layer setShadowColor:[UIColor grayColor].CGColor];

//

    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"shadowColor"];

    anim.duration = 1.f;

    anim.toValue = (id)[UIColor redColor].CGColor;

    anim.fromValue =  (id)[UIColor blackColor].CGColor;

     

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    anim.repeatCount = CGFLOAT_MAX;

    anim.autoreverses = YES;

     

    [ui_View.layer addAnimation:anim forKey:nil];

     

    CABasicAnimation *_anim = [CABasicAnimation animationWithKeyPath:@"shadowOffset"];

    _anim.duration = 1.f;

    _anim.fromValue = [NSValue valueWithCGSize:CGSizeMake(0,0)];

    _anim.toValue = [NSValue valueWithCGSize:CGSizeMake(3,3)];

     

    _anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    _anim.repeatCount = CGFLOAT_MAX;

    _anim.autoreverses = YES;

     

    [ui_View.layer addAnimation:_anim forKey:nil];

     

     

    CABasicAnimation *_anim1 = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];

    _anim1.duration = 1.f;

    _anim1.fromValue = [NSNumber numberWithFloat:0.5];

    _anim1.toValue = [NSNumber numberWithFloat:1];

     

    _anim1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    _anim1.repeatCount = CGFLOAT_MAX;

    _anim1.autoreverses = YES;

     

    [ui_View.layer addAnimation:_anim1 forKey:nil];

     

     

     

    CABasicAnimation *_anim2 = [CABasicAnimation animationWithKeyPath:@"shadowRadius"];

    _anim2.duration = 1.f;

    _anim2.fromValue = [NSNumber numberWithFloat:10];

    _anim2.toValue = [NSNumber numberWithFloat:5];

     

    _anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    _anim2.repeatCount = CGFLOAT_MAX;

    _anim2.autoreverses = YES;

     

    [ui_View.layer addAnimation:_anim2 forKey:nil];

 下面是一些应用

 

几个可以用来实现热门APP应用PATH中menu效果的几个方法

 

+(CABasicAnimation *)opacityForever_Animation:(float)time //永久闪烁的动画

 

{

 

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];

 

    animation.fromValue=[NSNumber numberWithFloat:1.0];

 

    animation.toValue=[NSNumber numberWithFloat:0.0];

 

    animation.autoreverses=YES;

 

    animation.duration=time;

 

    animation.repeatCount=FLT_MAX;

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    returnanimation;

 

}

 

  

 

+(CABasicAnimation *)opacityTimes_Animation:(float)repeatTimes durTimes:(float)time; //有闪烁次数的动画

 

{

 

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];

 

    animation.fromValue=[NSNumber numberWithFloat:1.0];

 

    animation.toValue=[NSNumber numberWithFloat:0.4];

 

    animation.repeatCount=repeatTimes;

 

    animation.duration=time;

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

 

    animation.autoreverses=YES;

 

    return animation;

 

}

 

  

 

+(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x //横向移动

 

{

 

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];

 

    animation.toValue=x;

 

    animation.duration=time;

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    returnanimation;

 

}

 

  

 

+(CABasicAnimation *)moveY:(float)time Y:(NSNumber *)y //纵向移动

 

{

 

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

 

    animation.toValue=y;

 

    animation.duration=time;

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    returnanimation;

 

}

 

  

 

+(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repeatTimes //缩放

 

{

 

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];

 

    animation.fromValue=orginMultiple;

 

    animation.toValue=Multiple;

 

    animation.duration=time;

 

    animation.autoreverses=YES;

 

    animation.repeatCount=repeatTimes;

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    returnanimation;

 

}

 

  

 

+(CAAnimationGroup *)groupAnimation:(NSArray *)animationAry durTimes:(float)time Rep:(float)repeatTimes //组合动画

 

{

 

    CAAnimationGroup *animation=[CAAnimationGroup animation];

 

    animation.animations=animationAry;

 

    animation.duration=time;

 

    animation.repeatCount=repeatTimes;

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    returnanimation;

 

}

 

  

 

+(CAKeyframeAnimation *)keyframeAniamtion:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes //路径动画

 

{

 

    CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];

 

    animation.path=path;

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

 

    animation.autoreverses=NO;

 

    animation.duration=time;

 

    animation.repeatCount=repeatTimes;

 

    returnanimation;

 

}

 

  

 

+(CABasicAnimation *)movepoint:(CGPoint )point //点移动

 

{

 

    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation"];

 

    animation.toValue=[NSValue valueWithCGPoint:point];

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    returnanimation;

 

}

 

  

 

+(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount //旋转

 

{

 

    CATransform3D rotationTransform  = CATransform3DMakeRotation(degree, 0, 0,direction);

 

    CABasicAnimation* animation;

 

    animation = [CABasicAnimation animationWithKeyPath:@"transform"];

 

  

 

animation.toValue= [NSValue valueWithCATransform3D:rotationTransform];

 

    animation.duration= dur;

 

animation.autoreverses= NO;

 

    animation.cumulative= YES;

 

    animation.removedOnCompletion=NO;

 

    animation.fillMode=kCAFillModeForwards;

 

    animation.repeatCount= repeatCount;

 

animation.delegate= self;

 

  

 

returnanimation;

 

}

 

 

 

 

 

 

 

 

 

 

AnimationPause

 

@implementation AnimationViewController

@synthesize controlButton;

@synthesize image;

 

- (void)dealloc

{

    [imagerelease];

   [controlButton release];

    [superdealloc];

}

 

- (void)didReceiveMemoryWarning

{

   // Releases the view if it doesn't have asuperview.

   [superdidReceiveMemoryWarning];

    

   // Release any cached data, images, etc thataren't in use.

}

 

- (void)addAnimations

{

  //让图片来回移

   CABasicAnimation *translation = [CABasicAnimationanimationWithKeyPath:@"position"];

    translation.fromValue= [NSValuevalueWithCGPoint:CGPointMake(24,240)];

    translation.toValue= [NSValuevalueWithCGPoint:CGPointMake(320-24,240)];

//    translation.toValue=[NSValuevalueWithCATransform3D:CATransform3DMakeRotation(3.1415, 0, 0, 1.0)];//效果

//   translation.toValue = [NSNumber numberWithDouble:1.5];//放效果

   translation.duration =2;//画持续时间

   translation.repeatCount =HUGE_VALF;//画重复次数

   translation.autoreverses =YES;//是否自重复

    

   //让图片来回转动

   CABasicAnimation *rotation = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];

   //kCAMediaTimingFunctionLinear 表示时间方法为线性,使得足球匀速转动

    rotation.timingFunction = [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];

    rotation.toValue = [NSNumbernumberWithFloat:4 * M_PI];

    rotation.duration =2;

    rotation.repeatCount =HUGE_VALF;

    rotation.autoreverses =YES;

    

    [image.layeraddAnimation:rotationforKey:@"rotation"];

    [image.layeraddAnimation:translationforKey:@"translation"];

}

 

#pragma mark - Viewlifecycle

- (void)viewDidLoad

{

    [super viewDidLoad];

    [self addAnimations];

}

 

 

- (void)viewDidUnload

{

    [selfsetImage:nil];

    [self setControlButton:nil];

    [super viewDidUnload];

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

   // Return YES for supported orientations

    return (interfaceOrientation ==UIInterfaceOrientationPortrait);

}

 

//layer上面的

- (void)pauseLayer:(CALayer*)layer

{

   CFTimeIntervalpausedTime = [layer convertTime:CACurrentMediaTime()fromLayer:nil];

    layer.speed =0.0;

    layer.timeOffset = pausedTime;

}

 

//继续layer上面的

- (void)resumeLayer:(CALayer*)layer

{

    CFTimeInterval pausedTime = [layertimeOffset];

    layer.speed =1.0;

    layer.timeOffset =0.0;

    layer.beginTime =0.0;

    CFTimeInterval timeSincePause = [layerconvertTime:CACurrentMediaTime()fromLayer:nil] -pausedTime;

    layer.beginTime = timeSincePause;

}

 

- (void)pauseSoccer

{

    isPause =YES;

    [controlButton setTitle:@"continue"forState:UIControlStateNormal];

    [selfpauseLayer:image.layer];

}

 

- (void)resumeSoccer

{

    isPause =NO;

    [controlButton setTitle:@"pause" forState:UIControlStateNormal];

    [self resumeLayer:image.layer];

}

 

- (IBAction)clickControlButton:(id)sender {

    if (isPause) {

       [selfresumeSoccer];

    }else{

       [selfpauseSoccer];

    }

}

@end

 

0 0
原创粉丝点击