ios - 带动画圆形旋转的进度条

本文介绍了一个自定义的圆形进度条控件的实现过程,包括如何设置进度条的颜色、宽度及图标等属性,并演示了如何在ViewController中使用该控件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <UIKit/UIKit.h>

@interface TJCircleProgressView : UIView
/**
 *  图标
 */
@property(nonatomic,strong)UIImage *imgIcon;
/**
 *  进度条值
 */
@property(nonatomic,assign)CGFloat progressValue;
/**
 *  进度条宽度
 */
@property(nonatomic,assign)CGFloat progressWidth;

/**
 *  进度条颜色
 */
@property(nonatomic,strong)UIColor *progressColor;

@end
#import "TJCircleProgressView.h"
@interface TJCircleProgressView ()
{
    UIBezierPath *circlePath;//布赛尔曲线
    CAShapeLayer *shapeLayer;// 圆形图层
    CAShapeLayer *imgLayer;//图标图层

}
@end
@implementation TJCircleProgressView
@synthesize progressColor = _progressColor;
@synthesize imgIcon = _imgIcon;
@synthesize progressValue = _progressValue;
@synthesize progressWidth = _progressWidth;

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        circlePath = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
        
        shapeLayer = [CAShapeLayer layer];
        shapeLayer.frame = self.bounds;
        shapeLayer.strokeColor = [UIColor redColor].CGColor;
        shapeLayer.fillColor   = [UIColor clearColor].CGColor;
        shapeLayer.path = circlePath.CGPath;
        
        shapeLayer.lineWidth   = 1.0f;
        [self.layer addSublayer:shapeLayer];
    }
    return self;
}
- (void)setImgIcon:(UIImage *)imgIcon
{
    _imgIcon = imgIcon;
    imgLayer = [CAShapeLayer layer];
    imgLayer.frame = CGRectMake(0, 0, imgIcon.size.width, imgIcon.size.height);
    imgLayer.contents = (__bridge id)imgIcon.CGImage;
    imgLayer.position = shapeLayer.position;
    [self.layer addSublayer:imgLayer];

}
- (void)setProgressValue:(CGFloat)progressValue
{
    _progressValue = progressValue;
    shapeLayer.strokeEnd = progressValue;
    [self startAnimation];
    /**延时4秒后移除动画以及view*/
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self stopAnimation];
    });
  }
- (void)setProgressWidth:(CGFloat)progressWidth
{
    _progressWidth = progressWidth;
    shapeLayer.lineWidth = progressWidth;
}
- (void)setProgressColor:(UIColor *)progressColor
{
    _progressColor = progressColor;
    shapeLayer.strokeColor = progressColor.CGColor;
}
-(void)startAnimation
{
    [shapeLayer removeAnimationForKey:@"RotationAnimation"];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = @(0);
    animation.toValue = @(M_PI * 2);
    animation.repeatCount = MAXFLOAT;
    animation.duration = 0.5f;
    animation.fillMode = kCAFillModeForwards;
    
    [shapeLayer addAnimation:animation forKey:@"RotationAnimation"];
}
- (void)stopAnimation
{
    [shapeLayer removeAllAnimations];
    [self removeFromSuperview];
}
@end
#import "ViewController.h"
#import "TJCircleProgressView.h"
@interface ViewController ()
{
    TJCircleProgressView *circleProgressView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    circleProgressView = [[TJCircleProgressView alloc]initWithFrame:CGRectMake(0,0, 100, 100)];
    circleProgressView.center = self.view.center;
    [circleProgressView setProgressValue:0.75];
    [circleProgressView setProgressColor:[UIColor grayColor]];
    [circleProgressView setProgressWidth:1.0f];
    [circleProgressView setImgIcon:[UIImage imageNamed:@"refresh"]];
    [self.view addSubview:circleProgressView];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ps:licecap录取动画没录好

转载于:https://www.cnblogs.com/thbbsky/p/4530493.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值