iOS 使用 CAShapeLayer 与贝塞尔曲线实现进度圆的动画

iOS 使用 CAShapeLayer 与贝塞尔曲线实现进度圆的动画

iOS 使用 CAShapeLayer 与贝塞尔曲线实现进度圆的动画

1.创建一个继承于 view 的 CircleView 视图

2.根据需求设置参数

/**

*  需要确定的参数 起始值 / 接收变化的值 / 边框宽 /边框颜色

*/

/**

*  起始值  (0 ~ 1)

*/

@property (nonatomic,assign) CGFloat startValue;

/**

*  线条宽

*/

@property (nonatomic,assign) CGFloat linewidth;

/**

*  线条颜色

*/

@property (nonatomic,strong) UIColor *lineColor;

/**

*  变化值

*/

@property (nonatomic,assign) CGFloat value;

- (void)startAniamtion;

- (void)endAnimation;



//3.在. m 文件

//创建一个延展

@interface CircleView ()

@property (nonatomic,strong) CAShapeLayer *shape;

@end



- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

_shape = [CAShapeLayer layer];

_shape.frame = self.bounds;

//创建出贝塞尔曲线

UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:self.bounds ];

_shape.path = circlePath.CGPath;

_shape.fillColor = [UIColor clearColor].CGColor;

_shape.lineWidth = 1.f;

_shape.strokeColor = [UIColor redColor].CGColor;

_shape.strokeEnd = 0.f;

[self.layer addSublayer:_shape];

}

return self;

}

@synthesize startValue = _startValue;

-(void)setStartValue:(CGFloat)startValue{

_startValue = startValue;

_shape.strokeEnd = startValue;

}

-(CGFloat)startValue{

return _startValue;

}

@synthesize linewidth = _linewidth;

-(void)setLinewidth:(CGFloat)linewidth{

_linewidth = linewidth;

_shape.lineWidth = linewidth;

}

-(CGFloat)linewidth{

return _linewidth;

}

@synthesize lineColor = _lineColor;

-(void)setLineColor:(UIColor *)lineColor{

_lineColor = lineColor;

_shape.strokeColor = lineColor.CGColor;

}

-(UIColor *)lineColor{

return _lineColor;

}

@synthesize value = _value;

- (void)setValue:(CGFloat)value{

_value = value;

_shape.strokeEnd = value;

}

-(CGFloat)value{

return _value;

}

- (void)startAniamtion{

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

animation.fromValue = @0;

animation.toValue = @1;

animation.repeatCount = MAX_CANON;

animation.duration = 5.f;

[_shape addAnimation:animation forKey:nil];

}



//4.ok, 这里已经创建完成,去控制器里面实现吧!

_circleView = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

_circleView.center = self.view.center;

_circleView.startValue = 0.f;

[self.view addSubview:_circleView];

[self performSelector:@selector(animationAction) withObject:nil afterDelay:2];



- (void)animationAction{

[_circleView startAniamtion];

}

//综上,只是简单的实现进度圆

 

转载于:https://my.oschina.net/alanTang123/blog/824901

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值