贝塞尔曲线

吃可爱长大的小学妹 提交于 2020-02-11 02:39:25

#import "MyView.h"

@interface MyView ()
@property (nonatomic, strong)UIBezierPath *path;
//放每次画的起点
@property (nonatomic, strong)NSMutableArray *paths;


@end

#define K_WIDTH [UIScreen mainScreen].bounds.size.width
#define K_HEIGHT [UIScreen mainScreen].bounds.size.height


@implementation MyView
//数组懒加载
-(NSMutableArray *)paths{
    if (_paths == nil) {
        _paths = [NSMutableArray array];
    }
    return _paths;
}

//获取到绘画的点
-(CGPoint)pathWithTouches:(NSSet *)touches{
    //获取点
    UITouch *touch = [touches anyObject];
    //把获取的点返回,加在画布上
    return [touch locationInView:self];
}

//开始画
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIBezierPath *path = [UIBezierPath bezierPath];
    _path = path;
    //每次开始画   都把起点存起来
    [self.paths addObject:path];
    //获取手指与屏幕的触摸点
    CGPoint point = [self pathWithTouches:touches];
    //划线
    [_path moveToPoint:point];
    
}
//移动
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [self pathWithTouches:touches];
    //连线
    [_path addLineToPoint:point];
    [self setNeedsDisplay];
}

//绘制的操作在下面的方法里执行
- (void)drawRect:(CGRect)rect {
    
    //创建贝塞尔对象
    //    _path = [UIBezierPath bezierPath];
    
#pragma mark ----------绘制直线组成的图形,比如:三角形,方形,五角星,六边形等等
    /*
     //确定起点
     [_path moveToPoint:CGPointMake(100, 100)];
     //确定绘制的终点
     [_path addLineToPoint:CGPointMake(200, 200)];
     [_path addLineToPoint:CGPointMake(100, 200)];
     
     //闭合
     [_path closePath];
     //设置填充颜色
     [[UIColor redColor] set];
     [_path fill];
     
     //线颜色
     [[UIColor yellowColor] set];
     */
    //绘制
    [_path stroke];
    
#pragma mark ----------绘制圆形
    /*
     //圆心
     CGPoint center = CGPointMake(K_WIDTH/2, K_HEIGHT/2);
     
     //半径
     CGFloat radius = 100.0;
     //起始角度
     CGFloat starA = - M_PI_2;
     //结束角度
     CGFloat endA = - M_PI_2 + _num * M_PI * 2;
     
     [_path addArcWithCenter:center radius:radius startAngle:starA endAngle:endA clockwise:YES];
     
     //设置线条宽度
     [_path setLineWidth:10];
     //绘制
     [_path stroke];
     */
    
    //绘制
    for (UIBezierPath *path in self.paths) {
        [path stroke];
    }
    
}

-(void)setNum:(CGFloat)num{
    _num = num;
    
    //重新走一遍drawRect方法;数值改变后,重新绘制图像
    [self setNeedsDisplay];
}

@end

 

#import "ViewController.h"
#import "MyView.h"

@interface ViewController ()

@property (nonatomic,strong)MyView *myView;

@property (nonatomic,strong)NSTimer *timer;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _myView = [[MyView alloc]initWithFrame:self.view.frame];
    
    _myView.backgroundColor = [UIColor cyanColor];
    
    [self.view addSubview:_myView];
    
    
    
    //开启定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerAC) userInfo:nil repeats:YES];
    
    
}


-(void)timerAC {
    _myView.num += .05;
    if (_myView.num == 1.0) {
        
        //下载完成,停止定时器
        [_timer invalidate];
    }
}





易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!