ios ~ 贝塞尔曲线:UIBezierPath和CAShapeLayer的折线图、曲线

WechatIMG20.jpeg
原理:UIBezierPathCAShapeLayer的使用:

其中太阳的图标,位置是 贝塞尔pathcurrentPoint(即当前的端点 = 终点)
iOS UIBezierPath(贝塞尔曲线) 中的方法和属性
CGPoint currentPoint = sunPath.currentPoint;
代码:

// Model 中的数据:
@property (nonatomic, assign) CGFloat   toSunset; // 日落:太阳☀️ (圆弧曲线)0~1(0 日出,1 日落)


//  GWCW_Club_ActualWeatherSunriseCell.m

//  日出 + 日落

#import "GWCW_Club_ActualWeatherSunriseCell.h"

@interface GWCW_Club_ActualWeatherSunriseCell ()

@property (nonatomic, strong) UIImageView   *sunriseImg;
@property (nonatomic, strong) UIImageView   *sunsetImg;
@property (nonatomic, strong) UILabel   *sunriseTimeL;
@property (nonatomic, strong) UILabel   *sunsetTimeL;
@property (nonatomic, strong) CAShapeLayer *sunLayer; // 父layer ,[self.sunLayer removeFromSuperlayer]; // 防止重用 在cell中时
@property (nonatomic, strong) UIImageView   *sunImg;  // 太阳图标

@end

@implementation GWCW_Club_ActualWeatherSunriseCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = ColorGlobalBalck;
        [self setupUI];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];
    self.sunriseImg.image = nil;
    self.sunriseTimeL.text = nil;
    self.sunsetImg.image = nil;
    self.sunsetTimeL.text = nil;
    [self.sunLayer removeFromSuperlayer]; // 防止重用 在cell中时
    self.sunImg.image = nil;
}

- (void)setWeatherModel:(GWActualMatchWeatherModel *)weatherModel {
    _weatherModel = weatherModel;
    
    self.sunriseImg.image = [UIImage imageNamed:@"new_weather_White日出_icon"];
    self.sunriseTimeL.text = _weatherModel.sunrise;
    self.sunsetImg.image = [UIImage imageNamed:@"new_weather_White日落_icon"];
    self.sunsetTimeL.text = _weatherModel.sunset;
    
    
    /// 贝塞尔曲线
    _sunLayer = [[CAShapeLayer alloc] init];
    _sunLayer.strokeColor = [UIColor clearColor].CGColor;
    
    UIBezierPath *bezierPath = [UIBezierPath
                                    bezierPathWithRoundedRect:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.frame.size.height)
                                    byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                    cornerRadii:CGSizeMake([UIScreen mainScreen].bounds.size.width/375*0, [UIScreen mainScreen].bounds.size.width/375*0)];

    _sunLayer.lineWidth = [UIScreen mainScreen].bounds.size.width/375*0.01;
    // 颜色
    _sunLayer.strokeColor = [UIColor clearColor].CGColor;
    // 背景填充色
    _sunLayer.fillColor = [UIColor clearColor].CGColor;
    _sunLayer.path = [bezierPath CGPath];
    
    [self.contentView.layer addSublayer:self.sunLayer];
    
    
    // 圆弧虚线
    CAShapeLayer *backLineLayer = [CAShapeLayer layer];
    // 线宽
    backLineLayer.lineWidth = [UIScreen mainScreen].bounds.size.width/375*1; // 10 - 6 = 4
    backLineLayer.lineCap = kCALineCapButt;  // 端点样式
    backLineLayer.lineJoin = kCALineCapButt; // 终点处理
    // 线条的颜色
    backLineLayer.strokeColor = [UIColor whiteColor].CGColor;
    // 背景填充色
    backLineLayer.fillColor = [UIColor clearColor].CGColor;
    // 设置线宽、线间距(虚线)
    [backLineLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], nil]];

    // 设置半径
    CGFloat backRadius = [UIScreen mainScreen].bounds.size.width/375*250/2;

    // 初始化一个路径:创建圆弧 ,startAngle:起始点,endAngle:终止点,clockwise:顺时针方向 ,M_PI == π:3.1415926
    // bezierPathWithArcCenter 中心点,下面就让addSublayer了,那么就设置self.bezierBackImg.layer的 中心点就好了,宽/2,高/2
    UIBezierPath *backPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.width/375*250/2+15) radius:backRadius startAngle:(1.25*M_PI) endAngle:(1.75*M_PI) clockwise:YES]; // 终止点(60%几率时):(2*0.6 - 0.25)*M_PI,clockwise 顺时针 YES, 逆时针 NO

    // 将UIBezierPath类转换成CGPath,类似于UIColor的CGColor
    backLineLayer.path = [backPath CGPath];
    [self.sunLayer addSublayer:backLineLayer];
    


    /// 贝塞尔曲线(进度)
    CAShapeLayer *progressLineLayer = [CAShapeLayer layer];
    // 线宽
    progressLineLayer.lineWidth = [UIScreen mainScreen].bounds.size.width/375*2; // 10 - 6 = 4
    progressLineLayer.lineCap = kCALineCapSquare;  // 端点样式
    progressLineLayer.lineJoin = kCALineCapSquare; // 终点处理
    // 线条的颜色
    progressLineLayer.strokeColor = RGBA(255, 196, 15, 1).CGColor;
    // 背景填充色
    progressLineLayer.fillColor = [UIColor clearColor].CGColor;
    // 设置线宽、线间距(虚线)
//        [progressLineLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:2], [NSNumber numberWithInt:2], nil]];
    // 设置半径
    CGFloat progressRadius = [UIScreen mainScreen].bounds.size.width/375*250/2;

    // 初始化一个路径:创建圆弧 ,startAngle:起始点,endAngle:终止点,clockwise:顺时针方向 ,M_PI == π:3.1415926
    // bezierPathWithArcCenter 中心点,下面就让addSublayer了,那么就设置self.bezierBackImg.layer的 中心点就好了,宽/2,高/2
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.width/375*250/2+15) radius:progressRadius startAngle:(1.25*M_PI) endAngle:((1.25 + 0.5*(_weatherModel.toSunset))*M_PI) clockwise:YES]; // 终止点(60%几率时):(2*0.6 - 0.25)*M_PI,clockwise 顺时针 YES, 逆时针 NO

//    UIBezierPath *progressStartPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.width/375*250/2+15) radius:progressRadius startAngle:(1.25*M_PI) endAngle:(1.25*M_PI) clockwise:YES];
    
    // 将UIBezierPath类转换成CGPath,类似于UIColor的CGColor
    progressLineLayer.path = [progressPath CGPath];
    [self.sunLayer addSublayer:progressLineLayer];
    

    // 当前path的位置,可以理解为path的终点:
    UIBezierPath *sunPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.width/375*250/2+15) radius:progressRadius startAngle:(1.25*M_PI) endAngle:((1.25 + 0.5*(_weatherModel.toSunset))*M_PI) clockwise:YES];
    CGPoint currentPoint = sunPath.currentPoint;
    _sunImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/375*15, [UIScreen mainScreen].bounds.size.width/375*15)];
//    self.sunImg.center = currentPoint;
    self.sunImg.backgroundColor = [UIColor clearColor];
    self.sunImg.image = [UIImage imageNamed:@"club_sun"];
    [self.contentView addSubview:self.sunImg];
    
    
    // 开始位置
    UIBezierPath *sunStartPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.width/375*250/2+15) radius:progressRadius startAngle:(1.25*M_PI) endAngle:(1.25*M_PI) clockwise:YES];
    CGPoint startPoint = sunStartPath.currentPoint;
    self.sunImg.center = startPoint;

    // 添加动画效果
    CAKeyframeAnimation* keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyFrameAni.repeatCount = 1;
    keyFrameAni.path = sunPath.CGPath;
    keyFrameAni.duration = 2;
    keyFrameAni.beginTime = CACurrentMediaTime();
    // fillMode主要是决定显示layer在动画完成后的状态..一般和removedOnCompletion一起使用..
    keyFrameAni.fillMode = kCAFillModeForwards;
    keyFrameAni.removedOnCompletion = NO; // YES 动画完成后会回到原始状态,NO 动画完成后会保持状态
    [self.sunImg.layer addAnimation:keyFrameAni forKey:@"keyFrameAnimation"];
    
    // 线条 动画效果
    CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnima.duration = 2.0f;//动画时间
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnima.fromValue = [NSNumber numberWithFloat:0.0f];//开始点
    pathAnima.toValue = [NSNumber numberWithFloat:1.0f];//结束点
    pathAnima.fillMode = kCAFillModeForwards;
    pathAnima.removedOnCompletion = NO;
    [progressLineLayer addAnimation:pathAnima forKey:@"strokeEnd"];
    
}

- (void)setupUI {
    
    _sunriseImg = [[UIImageView alloc] init];
    self.sunriseImg.image = [UIImage imageNamed:@"new_weather_White日出_icon"];
    [self.contentView addSubview:self.sunriseImg];
    [self.sunriseImg makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*20);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-[UIScreen mainScreen].bounds.size.width/375*16);
        make.width.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*14);
    }];
    
    _sunriseTimeL = [[UILabel alloc] init];
    _sunriseTimeL.textColor = [UIColor whiteColor];
    _sunriseTimeL.textAlignment = NSTextAlignmentLeft;
    _sunriseTimeL.font = [UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*13 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.sunriseTimeL];
    [self.sunriseTimeL makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.sunriseImg);
        make.left.mas_equalTo(self.sunriseImg.mas_right).offset([UIScreen mainScreen].bounds.size.width/375*6);
    }];
    
    _sunsetImg = [[UIImageView alloc] init];
    self.sunsetImg.image = [UIImage imageNamed:@"new_weather_White日落_icon"];
    [self.contentView addSubview:self.sunsetImg];
    [self.sunsetImg makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.sunriseImg);
        make.right.mas_equalTo(self.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*72);
        make.width.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*14);
    }];
    
    _sunsetTimeL = [[UILabel alloc] init];
    _sunsetTimeL.textColor = [UIColor whiteColor];
    _sunsetTimeL.textAlignment = NSTextAlignmentLeft;
    _sunsetTimeL.font = [UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*13 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.sunsetTimeL];
    [self.sunsetTimeL makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.sunriseImg);
        make.left.mas_equalTo(self.sunsetImg.mas_right).offset([UIScreen mainScreen].bounds.size.width/375*6);
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容