您的位置:首页 > 其它

视图背景颜色渐变、画圆、动画

2016-06-01 14:20 267 查看
代码:

在继承于UIView中使用

// 生产一个圆形路径并设置成遮罩
self.layer.mask = [self produceCircleShapeLayer];


+ (Class)layerClass
{
return [CAGradientLayer class];
}


把颜色添加到数组里面

- (void)setupMulticolor
{
CAGradientLayer *gradientLayer = (id)[self layer];

// 设置颜色线条分布的方向
gradientLayer.startPoint = CGPointMake(0.0, 0.0);
gradientLayer.endPoint = CGPointMake(1.0, 0.0);

// 创建颜色数组
NSMutableArray *colors = [NSMutableArray array];
for (NSInteger hue = 0; hue <= 360; hue += 10) {
[colors addObject:(id)[UIColor colorWithHue:1.0*hue/360.0 saturation:1.0 brightness:1.0 alpha:1.0].CGColor];
}

[gradientLayer setColors:[NSArray arrayWithArray:colors]];
}


画一个圆代码:

- (CAShapeLayer *)produceCircleShapeLayer
{
// 生产出一个圆的路径
CGPoint circleCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
CGFloat circleRadius = self.bounds.size.width/2.0 - 40;

UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:circleCenter
radius:circleRadius
startAngle:M_PI
endAngle:-M_PI
clockwise:NO];

// 生产出一个圆形路径的Layer
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = circlePath.CGPath;
shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
shapeLayer.lineWidth = 2;

// 可以设置出圆的完整性
shapeLayer.strokeStart = 0;
shapeLayer.strokeEnd = 1.0;

return shapeLayer;
}


动画旋转代码:通过改变transform.rotation.y来改变动画的方向

transform.rotation.y纵轴上旋转

transform.rotation.x横轴上旋转

transform.rotation.z围绕着z纵轴旋转

- (void)startAnimation
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
animation.duration = 5;
animation.repeatCount = MAXFLOAT;
animation.fromValue = [NSNumber numberWithDouble:0];
animation.toValue = [NSNumber numberWithDouble:M_PI*2];
[self.layer addAnimation:animation forKey:@"transform"];
}


当动画结束时调用:

- (void)endAnimation
{
[self.layer removeAllAnimations];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: