圆环旋转加载动画

给需要的朋友,思路还是比较简单,代码比较直接,没有封装,复制粘贴即可看到效果。

圆环旋转加载动画_第1张图片
屏幕快照 2017-01-09 下午4.00.58.png

  • (void)creatText{

    NSArray * colours = [NSArray arrayWithArray:[self getColoures]];

    UIColor * colour0 = [UIColor new];
    UIColor * colour1 = [UIColor new];
    UIColor * colour2 = [UIColor new];
    UIColor * colour3 = [UIColor new];

pragma mark- gradientlayer 与 gradientlayer 之间颜色过渡

colour0 = colours[0];
colour1 = colours[1];
colour2 = colours[2];
colour3 = colours[3];


CALayer * layer0 = [CALayer layer];
layer0.frame = CGRectMake(20, 150, 150, 150);
[self.view.layer addSublayer:layer0];

CALayer * layer1 = [CALayer layer];
layer1.frame = CGRectMake(0, 0, 150, 150);
[layer0 addSublayer:layer1];

pragma mark-4个gradientlayer颜色渐变

CAGradientLayer * gradientlayer1 = [CAGradientLayer layer];
gradientlayer1.frame = CGRectMake(0, 0, 75, 75);

gradientlayer1.colors = @[(__bridge id)colour0.CGColor,
                          (__bridge id)colour1.CGColor];

gradientlayer1.startPoint = CGPointMake(1, 0);
gradientlayer1.endPoint = CGPointMake(0, 1);


CAGradientLayer * gradientlayer2 = [CAGradientLayer layer];
gradientlayer2.frame = CGRectMake(0, 75, 75, 75);

gradientlayer2.colors = @[(__bridge id)colour1.CGColor,
                          (__bridge id)colour2.CGColor];

gradientlayer2.startPoint = CGPointMake(0, 0);
gradientlayer2.endPoint = CGPointMake(1,1);

CAGradientLayer * gradientlayer3 = [CAGradientLayer layer];
gradientlayer3.frame = CGRectMake(75, 75, 75, 75);

gradientlayer3.colors = @[(__bridge id)colour2.CGColor,
                          (__bridge id)colour3.CGColor];

gradientlayer3.startPoint = CGPointMake(0, 1);
gradientlayer3.endPoint = CGPointMake(1, 0);

CAGradientLayer * gradientlayer4 = [CAGradientLayer layer];
gradientlayer4.frame = CGRectMake(75, 0, 75, 75);

gradientlayer4.colors = @[(__bridge id)colour3.CGColor,
                          (__bridge id)[UIColor whiteColor].CGColor];

gradientlayer4.startPoint = CGPointMake(1, 1);
gradientlayer4.endPoint = CGPointMake(0, 0);

[layer1 addSublayer:gradientlayer1];
[layer1 addSublayer:gradientlayer2];
[layer1 addSublayer:gradientlayer3];
[layer1 addSublayer:gradientlayer4];

pragma mark- 把下面代码注释掉,就可以看到4个人gradientlayer颜色过渡效果

//路径
UIBezierPath * beizierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(75, 75) radius:70 startAngle: -M_PI/2 endAngle:M_PI*2 -M_PI/2 clockwise:YES];
CAShapeLayer * shaperLayer = [CAShapeLayer layer];
shaperLayer.lineCap = kCALineCapRound;
shaperLayer.fillColor = [UIColor clearColor].CGColor;
shaperLayer.strokeColor = [UIColor redColor].CGColor;
shaperLayer.lineWidth = 5.0;
shaperLayer.strokeStart = 0.01;
shaperLayer.strokeEnd = 0.99;
shaperLayer.path = beizierPath.CGPath;
layer1.mask = shaperLayer;


//添加动画
CABasicAnimation * basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
basicAnimation.duration = 1;
basicAnimation.fromValue = @0.0;
basicAnimation.toValue = @(M_PI*2);
basicAnimation.repeatCount = HUGE_VAL;

[layer0 addAnimation:basicAnimation forKey:@"transform.rotation.z"];

}

pragma mark- 获取颜色

  • (NSArray *)getColoures{

    NSMutableArray * array = [NSMutableArray new];

    UIColor * redcolour = [UIColor colorWithHue:136.0 saturation:1.0 brightness:1.0 alpha:1];

    //同一种颜色的不同饱和度(这里取4种)
    for (int i = 0; i < 4; i++) {

      CGFloat hue = 0.0;
    
      CGFloat saturation = 0.0;
      
      CGFloat brightness = 0.0;
      
      CGFloat alpha = 0.0;
    
      
      [redcolour getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
      UIColor * newColour = [UIColor colorWithHue:hue saturation: saturation - (i*0.25) brightness:brightness alpha:alpha];
      
      NSLog(@"%f,%f,%f,%f",hue,saturation,brightness,alpha);
    
      [array addObject:newColour];
    

    }

    return array;
    }

你可能感兴趣的:(圆环旋转加载动画)