封装了一个iOS环形加载动画和成功对号效果

这里要注意一个地方,如果我们这里不设置

_checkLayer.fillColor = [UIColor clearColor].CGColor;

的话,就会造成一个后果,就是,从七点到终点和走过的line中间的地方会有黑色,所以这里要特别注意,fillColor 设置成clearcolor

如果不设置的话,效果就是下面的样子

代码在这里

//
//  LBLoadingCheckView.m
//  TEXT
//
//  Created by mac on 2024/5/22.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "LBLoadingCheckView.h"

@interface LBLoadingCheckView ()

@property (nonatomic, strong) CAShapeLayer *progressLayer;

@property (nonatomic, strong) UIView *checkView;

@property (nonatomic, strong) CAShapeLayer *checkLayer;

@property (nonatomic, strong) CABasicAnimation *animation;

@end


@implementation LBLoadingCheckView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    self.frame = CGRectMake(0, 0, 37, 47);
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 4;
    self.backgroundColor = [UIColor blackColor];
    [self.layer addSublayer:self.progressLayer];
    [self addSubview:self.checkView];
    [self.checkView.layer addSublayer:self.checkLayer];
}

- (void)showProgress:(CGFloat)progress
{
    self.progressLayer.strokeEnd = progress;
    if (progress >= 0.99) {
        self.progressLayer.hidden = YES;
        self.checkView.hidden = NO;
        [self.checkLayer addAnimation:self.animation forKey:@"strokeEnd"];
    }
}

#pragma mark - lazy load

- (CAShapeLayer *)progressLayer
{
    if (!_progressLayer) {
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(18.25, 30) radius:11 startAngle:-M_PI_2 endAngle:-M_PI_2 + M_PI * 2 clockwise:YES];
        _progressLayer = [[CAShapeLayer alloc] init];
        _progressLayer.strokeColor = [UIColor whiteColor].CGColor;
        _progressLayer.fillColor = [UIColor clearColor].CGColor;
        _progressLayer.lineWidth = 2;
        _progressLayer.frame = CGRectMake(0, 0, 32.5, 43);
        _progressLayer.path = path.CGPath;
        _progressLayer.strokeStart = 0;
        _progressLayer.strokeEnd = 1;
    }
    return _progressLayer;
}

- (UIView *)checkView
{
    if (!_checkView) {
        _checkView = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 20, 20)];
        _checkView.backgroundColor = [UIColor whiteColor];
        _checkView.layer.masksToBounds = YES;
        _checkView.layer.cornerRadius = 10;
        _checkView.hidden = YES;
        _checkView.backgroundColor = [UIColor greenColor];
    }
    return _checkView;
}

- (CAShapeLayer *)checkLayer
{
    if (!_checkLayer) {
        UIBezierPath *path = [[UIBezierPath alloc] init];
        path.lineCapStyle = kCGLineCapRound;
        path.lineJoinStyle = kCGLineJoinRound;
        [path moveToPoint:CGPointMake(5, 11)];
        CGPoint pl = CGPointMake(9, 14);
        [path addLineToPoint:pl];
        
        CGPoint p2 = CGPointMake(15, 6);
        [path addLineToPoint:p2];
        _checkLayer = [[CAShapeLayer alloc] init];
        _checkLayer.fillColor = [UIColor clearColor].CGColor;
        _checkLayer.strokeColor = [UIColor blackColor].CGColor;
        _checkLayer.lineWidth = 2;
        _checkLayer.path = path.CGPath;
    }
    return _checkLayer;
}

- (CABasicAnimation *)animation
{
    if (!_animation) {
        _animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        _animation.fromValue = @(0);
        _animation.toValue = @(1);
        _animation.duration = 0.5;
    }
    return _animation;
}

@end
相关推荐
OKXLIN3 小时前
IOS UITextField 无法隐藏键盘问题
ios·objective-c
Macdo_cn11 小时前
My Metronome for Mac v1.4.2 我的节拍器 支持M、Intel芯片
macos·音视频
AL.千灯学长11 小时前
DeepSeek接入Siri(已升级支持苹果手表)完整版硅基流动DeepSeek-R1部署
人工智能·gpt·ios·ai·苹果vision pro
吹泡泡的派大星12 小时前
从0-1搭建mac环境最新版
macos
zhouwu_linux12 小时前
MT7628基于原厂的SDK包, 修改ra1网卡的MAC方法。
linux·运维·macos
丁总学Java12 小时前
在 macOS 的 ARM 架构上按住 Command (⌘) + Shift + .(点)。这将暂时显示隐藏文件和文件夹。
macos
青木川崎12 小时前
Mac下常用命令
macos
ClaNNEd@12 小时前
Mac端homebrew安装配置
macos·brew
nicekwell13 小时前
macos sequoia 禁用 ctrl+enter 打开鼠标右键菜单功能
macos
丁总学Java17 小时前
在 Mac ARM 架构的 macOS 系统上启用 F1 键作为 Snipaste 的截屏快捷键
macos·snipaste