zoukankan      html  css  js  c++  java
  • iOS简单动画效果:闪烁、移动、旋转、路径、组合

    1. #define kDegreesToRadian(x) (M_PI * (x) / 180.0)  
    2.    
    3. #define kRadianToDegrees(radian) (radian*180.0)/(M_PI)  
    4.    
    5. - (void)viewDidLoad  
    6. {  
    7.     [superviewDidLoad];  
    8.     self.title = @"测试动画";  
    9.     self.view.backgroundColor = [UIColorlightGrayColor];  
    10.       
    11.       
    12.     myTest1 = [[UILabelalloc]initWithFrame:CGRectMake(10, 100, 60, 40)];  
    13.     myTest1.backgroundColor = [UIColorblueColor];  
    14.     myTest1.textAlignment = NSTextAlignmentCenter;  
    15.     myTest1.text = @"张明炜";  
    16.     myTest1.textColor = [UIColorwhiteColor];  
    17.     [self.viewaddSubview:myTest1];  
    18.       
    19.       //闪烁效果。  
    20. //    [myTest1.layer addAnimation:[self opacityForever_Animation:0.5] forKey:nil];  
    21.       ///移动的动画。  
    22. //    [myTest1.layer addAnimation:[self moveX:1.0f X:[NSNumber numberWithFloat:200.0f]] forKey:nil];  
    23.     //缩放效果。  
    24. //    [myTest1.layer addAnimation:[self scale:[NSNumber numberWithFloat:1.0f] orgin:[NSNumber numberWithFloat:3.0f] durTimes:2.0f Rep:MAXFLOAT] forKey:nil];  
    25.      //组合动画。  
    26. //    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];  
    27. //    [myTest1.layer addAnimation:[self groupAnimation:myArray durTimes:3.0f Rep:MAXFLOAT] forKey:nil];  
    28.     //路径动画。  
    29. //    CGMutablePathRef myPah = CGPathCreateMutable();  
    30. //    CGPathMoveToPoint(myPah, nil,30, 77);  
    31. //    CGPathAddCurveToPoint(myPah, nil, 50, 50, 60, 200, 200, 200);//这里的是控制点。  
    32. //    [myTest1.layer addAnimation:[self keyframeAnimation:myPah durTimes:5 Rep:MAXFLOAT] forKey:nil];  
    33.     //旋转动画。  
    34.     [myTest1.layeraddAnimation:[selfrotation:2degree:kRadianToDegrees(90) direction:1repeatCount:MAXFLOAT] forKey:nil];  
    35.       
    36.       
    37. }  
    38.    
    39. #pragma mark === 永久闪烁的动画 ======  
    40. -(CABasicAnimation *)opacityForever_Animation:(float)time  
    41. {  
    42.     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。  
    43.     animation.fromValue = [NSNumber numberWithFloat:1.0f];  
    44.     animation.toValue = [NSNumber numberWithFloat:0.0f];//这是透明度。  
    45.     animation.autoreverses = YES;  
    46.     animation.duration = time;  
    47.     animation.repeatCount = MAXFLOAT;  
    48.     animation.removedOnCompletion = NO;  
    49.     animation.fillMode = kCAFillModeForwards;  
    50.      animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。  
    51.     return animation;  
    52. }  
    53.    
    54. #pragma mark =====横向、纵向移动===========  
    55. -(CABasicAnimation *)moveX:(float)time X:(NSNumber *)x  
    56. {  
    57.     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];///.y的话就向下移动。  
    58.     animation.toValue = x;  
    59.     animation.duration = time;  
    60.     animation.removedOnCompletion = NO;//yes的话,又返回原位置了。  
    61.     animation.repeatCount = MAXFLOAT;  
    62.     animation.fillMode = kCAFillModeForwards;  
    63.     return animation;  
    64. }  
    65.    
    66. #pragma mark =====缩放-=============  
    67. -(CABasicAnimation *)scale:(NSNumber *)Multiple orgin:(NSNumber *)orginMultiple durTimes:(float)time Rep:(float)repertTimes  
    68. {  
    69.     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];  
    70.     animation.fromValue = Multiple;  
    71.     animation.toValue = orginMultiple;  
    72.     animation.autoreverses = YES;  
    73.     animation.repeatCount = repertTimes;  
    74.     animation.duration = time;//不设置时候的话,有一个默认的缩放时间.  
    75.     animation.removedOnCompletion = NO;  
    76.     animation.fillMode = kCAFillModeForwards;  
    77.     return  animation;  
    78. }  
    79.    
    80. #pragma mark =====组合动画-=============  
    81. -(CAAnimationGroup *)groupAnimation:(NSArray *)animationAry durTimes:(float)time Rep:(float)repeatTimes  
    82. {  
    83.     CAAnimationGroup *animation = [CAAnimationGroupanimation];  
    84.     animation.animations = animationAry;  
    85.     animation.duration = time;  
    86.     animation.removedOnCompletion = NO;  
    87.     animation.repeatCount = repeatTimes;  
    88.     animation.fillMode = kCAFillModeForwards;  
    89.     return animation;  
    90. }  
    91.    
    92. #pragma mark =====路径动画-=============  
    93. -(CAKeyframeAnimation *)keyframeAnimation:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes  
    94. {  
    95.     CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];  
    96.     animation.path = path;  
    97.     animation.removedOnCompletion = NO;  
    98.     animation.fillMode = kCAFillModeForwards;  
    99.     animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];  
    100.     animation.autoreverses = NO;  
    101.     animation.duration = time;  
    102.     animation.repeatCount = repeatTimes;  
    103.     return animation;  
    104. }  
    105.    
    106. #pragma mark ====旋转动画======  
    107. -(CABasicAnimation *)rotation:(float)dur degree:(float)degree direction:(int)direction repeatCount:(int)repeatCount  
    108. {  
    109.     CATransform3D rotationTransform = CATransform3DMakeRotation(degree, 0, 0, direction);  
    110.     CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];  
    111.     animation.toValue = [NSValue valueWithCATransform3D:rotationTransform];  
    112.     animation.duration  =  dur;  
    113.     animation.autoreverses = NO;  
    114.     animation.cumulative = NO;  
    115.     animation.fillMode = kCAFillModeForwards;  
    116.     animation.repeatCount = repeatCount;  
    117.     animation.delegate = self;  
    118.    
    119.     return animation;  
    120.    
  • 相关阅读:
    [LeetCode] Exclusive Time of Functions 函数的独家时间
    [LeetCode] Design Log Storage System 设计日志存储系统
    [LeetCode] Find the Derangement of An Array 找数组的错排
    [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间
    [LeetCode] 633. Sum of Square Numbers 平方数之和
    [LeetCode] Design Excel Sum Formula 设计Excel表格求和公式
    [LeetCode] Delete Operation for Two Strings 两个字符串的删除操作
    [LeetCode] Kill Process 结束进程
    [LeetCode] Course Schedule III 课程清单之三
    [LeetCode] K Inverse Pairs Array K个翻转对数组
  • 原文地址:https://www.cnblogs.com/wzdevelop/p/7298394.html
Copyright © 2011-2022 走看看