用CAShapeLayer写股市K线图动画效果

本文介绍如何使用CAShapeLayer创建股市K线图动画效果。通过生成贝塞尔曲线并利用NSTimer更新strokeEnd属性实现动态绘制过程。文章提供具体代码示例。

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

用CAShapeLayer写股市K线图动画效果

 

说明

入市有风险,炒股需谨慎。(因项目需求,本人提供了写这种效果的源码)

 

效果

 

源码

//
//  ViewController.m
//  Path
//
//  Created by YouXianMing on 15/5/11.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSTimer       *timer;
@property (nonatomic, strong) CAShapeLayer  *shapeLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    
    // 生成贝塞尔曲线
    UIBezierPath* bezierPath = UIBezierPath.bezierPath;
    [bezierPath moveToPoint: CGPointMake(4.5, 2.5)];
    [bezierPath addLineToPoint: CGPointMake(32.5, 59.5)];
    [bezierPath addLineToPoint: CGPointMake(79.5, 10.5)];
    [bezierPath addLineToPoint: CGPointMake(144.5, 69.5)];
    [bezierPath addLineToPoint: CGPointMake(175.5, 15.5)];
    [bezierPath addLineToPoint: CGPointMake(211.5, 48.5)];
    [bezierPath addLineToPoint: CGPointMake(217.5, 23.5)];
    
    
    // 产生layer
    self.shapeLayer             = [CAShapeLayer layer];
    self.shapeLayer.frame       = CGRectMake(0, 0, 200, 200);
    self.shapeLayer.fillColor   = [UIColor clearColor].CGColor;
    self.shapeLayer.strokeColor = [UIColor redColor].CGColor;
    self.shapeLayer.borderWidth = 1.f;
    self.shapeLayer.path        = bezierPath.CGPath;
    
    
    // 添加layer
    [self.view.layer addSublayer:self.shapeLayer];
    
    
    // 添加定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f
                                                  target:self
                                                selector:@selector(timerEvent)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void)timerEvent {
    CGFloat percent           = arc4random() % 100 / 100.f;
    self.shapeLayer.strokeEnd = percent;
    NSLog(@"%.1f%%", percent * 100);
}



@end

 

细节

贝塞尔曲线的坐标原点是以shapeLayer的(0,0)点计算的,注意哦,亲!

 

转载于:https://www.cnblogs.com/YouXianMing/p/4495754.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值