IOS 基本动画(旋转、移动、闪烁、缩放等)

#define kDegreesToRadian(x) (M_PI * (x) / 180.0)

#define kRadianToDegrees(radian) (radian*180.0)/(M_PI)

- (void)viewDidLoad

{

 
  1. \[superviewDidLoad\];
  2. self.title = @"测试动画";
  3. self.view.backgroundColor = \[UIColorlightGrayColor\];
  4. myTest1 = \[\[UILabelalloc\]initWithFrame:CGRectMake(10, 100, 60, 40)\];
  5. myTest1.backgroundColor = \[UIColorblueColor\];
  6. myTest1.textAlignment = NSTextAlignmentCenter;
  7. myTest1.text = @"测试动画";
  8. myTest1.textColor = \[UIColorwhiteColor\];
  9. \[self.viewaddSubview:myTest1\];
  10. //闪烁效果。

// [myTest1.layer addAnimation:[self opacityForever_Animation:0.5] forKey:nil];

 
  1. ///移动的动画。

// [myTest1.layer addAnimation:[self moveX:1.0f X:[NSNumber numberWithFloat:200.0f]] forKey:nil];

 
  1. //缩放效果。

// [myTest1.layer addAnimation:[self scale:[NSNumber numberWithFloat:1.0f] orgin:[NSNumber numberWithFloat:3.0f] durTimes:2.0f Rep:MAXFLOAT] forKey:nil];

 
  1. //组合动画。

// NSArray *myArray = [NSArray arrayWithObjects:[self opacityForever_Animation:0.5],[self moveX:1.0f X:[NSNumber numberWithFloat:200.0f]],[self scale:[NSNumber numberWithFloat:1.0f] orgin:[NSNumber numberWithFloat:3.0f] durTimes:2.0f Rep:MAXFLOAT], nil];

// [myTest1.layer addAnimation:[self groupAnimation:myArray durTimes:3.0f Rep:MAXFLOAT] forKey:nil];

 
  1. //路径动画。

// CGMutablePathRef myPah = CGPathCreateMutable();

// CGPathMoveToPoint(myPah, nil,30, 77);

// CGPathAddCurveToPoint(myPah, nil, 50, 50, 60, 200, 200, 200);//这里的是控制点。

// [myTest1.layer addAnimation:[self keyframeAnimation:myPah durTimes:5 Rep:MAXFLOAT] forKey:nil];

 
  1. //旋转动画。
  2. \[myTest1.layeraddAnimation:\[selfrotation:2degree:kRadianToDegrees(90)direction:1repeatCount:MAXFLOAT\] forKey:nil\];

}

#pragma mark === 永久闪烁的动画 ======

-(CABasicACnimation *)opacityForever_Animation:(float)time

{

 
  1. CABasicAnimation \*animation = \[CABasicAnimationanimationWithKeyPath:@"opacity"\];//必须写opacity才行。
  2. animation.fromValue = \[NSNumbernumberWithFloat:1.0f\];
  3. animation.toValue = \[NSNumbernumberWithFloat:0.0f\];//这是透明度。
  4. animation.autoreverses = YES;
  5. animation.duration = time;
  6. animation.repeatCount = MAXFLOAT;
  7. animation.removedOnCompletion = NO;
  8. animation.fillMode = kCAFillModeForwards;
  9. animation.timingFunction=\[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn\];///没有的话是均匀的动画。
  10. return animation;

}

#pragma mark =====横向、纵向移动===========

-(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x

{

 
  1. CABasicAnimation \*animation = \[CABasicAnimationanimationWithKeyPath:@"transform.translation.x"\];///.y的话就向下移动。
  2. animation.toValue = x;
  3. animation.duration = time;
  4. animation.removedOnCompletion = NO;//yes的话,又返回原位置了。
  5. animation.repeatCount = MAXFLOAT;
  6. animation.fillMode = kCAFillModeForwards;
  7. return animation;

}

#pragma mark =====缩放-=============

-(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repertTimes

{

 
  1. CABasicAnimation \*animation = \[CABasicAnimationanimationWithKeyPath:@"transform.scale"\];
  2. animation.fromValue = Multiple;
  3. animation.toValue = orginMultiple;
  4. animation.autoreverses = YES;
  5. animation.repeatCount = repertTimes;
  6. animation.duration = time;//不设置时候的话,有一个默认的缩放时间.
  7. animation.removedOnCompletion = NO;
  8. animation.fillMode = kCAFillModeForwards;
  9. return animation;

}

#pragma mark =====组合动画-=============

-(CAAnimationGroup *)groupAnimation:(NSArray *)animationAry durTimes:(float)time Rep:(float)repeatTimes

{

 
  1. CAAnimationGroup \*animation = \[CAAnimationGroupanimation\];
  2. animation.animations = animationAry;
  3. animation.duration = time;
  4. animation.removedOnCompletion = NO;
  5. animation.repeatCount = repeatTimes;
  6. animation.fillMode = kCAFillModeForwards;
  7. return animation;

}

#pragma mark =====路径动画-=============

-(CAKeyframeAnimation *)keyframeAnimation:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes

{

 
  1. CAKeyframeAnimation \*animation = \[CAKeyframeAnimationanimationWithKeyPath:@"position"\];
  2. animation.path = path;
  3. animation.removedOnCompletion = NO;
  4. animation.fillMode = kCAFillModeForwards;
  5. animation.timingFunction = \[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn\];
  6. animation.autoreverses = NO;
  7. animation.duration = time;
  8. animation.repeatCount = repeatTimes;
  9. return animation;

}

#pragma mark ====旋转动画======

-(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount

{

 
  1. CATransform3D rotationTransform = CATransform3DMakeRotation(degree, 0, 0, direction);
  2. CABasicAnimation \*animation = \[CABasicAnimationanimationWithKeyPath:@"transform"\];
  3. animation.toValue = \[NSValue valueWithCATransform3D:rotationTransform\];
  4. animation.duration = dur;
  5. animation.autoreverses = NO;
  6. animation.cumulative = NO;
  7. animation.fillMode = kCAFillModeForwards;
  8. animation.repeatCount = repeatCount;
  9. animation.delegate = self;
  10. return animation;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值