最简单的画方画圆 iOS

2019-07-23  本文已影响0人  coding_Liu
//  ViewController.m
//  画方画圆
//
//  Created by Admin on 2019/5/5.
//  Copyright © 2019年 JZY. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIView *v;

@end

@implementation ViewController

{
    
    CAShapeLayer *shapeLayer;
    CGPoint startPoint;
    CGPoint endPoint;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview: self.v];
    self.v.frame = CGRectMake(50, 50, 200, 200);
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    shapeLayer = [CAShapeLayer new];
    shapeLayer.backgroundColor = [UIColor purpleColor].CGColor;
    shapeLayer.fillColor = [UIColor whiteColor].CGColor; //填充颜色
    shapeLayer.strokeColor = [UIColor redColor].CGColor; //边框颜色
    shapeLayer.lineWidth = 2.0f; //边框的宽度
    [self.v.layer addSublayer:shapeLayer];
    self.v.layer.backgroundColor = UIColor.greenColor.CGColor;
    
    UITouch *touch  = [touches anyObject];
    startPoint = [touch locationInView:self.v];
//    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(80, 150) radius:80 startAngle:0 endAngle:M_PI*2 clockwise:YES];
//    shapeLayer.path = bezierPath.CGPath;
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
   
    UITouch *touch  = [touches anyObject];
    endPoint = [touch locationInView:self.v];

    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y)];

    
    //bezierPathWithOvalInRect 圆
//    bezierPathWithRect 方
    
    
    
    
    
//    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(point.x, point.y) radius:80 startAngle:0 endAngle:M_PI*2 clockwise:YES];

    shapeLayer.path = bezierPath.CGPath;

}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
}

- (UIView *)v {
    if (!_v) {
        _v = [UIView new];
        _v.backgroundColor = [UIColor grayColor];
    }
    return _v;
}

@end
上一篇 下一篇

猜你喜欢

热点阅读