iOSの贝塞尔曲线的创建以及controlPoint动态调整曲线

2016-07-19  本文已影响687人  一束橘子

首先推荐一下一直在看的博客文顶顶的博客基本上涵盖了iOS入门到中高级的所有知识点,而且极度详细。是的,极度!!
如果老爷们有哪里忘了尽管去找,包你找到。

贝塞尔曲线,其实了解的很皮毛,所以记录一下总结一下。
概念我就不写了,怕自己理解的有偏颇

一、创建
+ (instancetype)bezierPath;
+ (instancetype)bezierPathWithRect:(CGRect)rect;//矩形
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;//内切圆或者椭圆
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; //带圆角的矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;//画矩形,指定圆角角
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;//画圆弧
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;//根据路径划线

- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

二、Talk is cheap!Show you my code!
1.创建继承UIView的子类
2.重写drawRect: 方法(创建路径-绘图)
#import "PaintView.h"
@interface PaintView()
@property (nonatomic,strong) NSMutableArray<UIView *> *pointArray;
@property (nonatomic,strong) UIBezierPath *bezierPath;
@end
@implementation PaintView
-(NSMutableArray *)pointArray
{
if (!_pointArray) {
_pointArray = [NSMutableArray array];
}
return _pointArray;
}
-(UIBezierPath *)bezierPath
{
if (!_bezierPath) {
UIColor *lineColor = [UIColor orangeColor];
[lineColor set];
_bezierPath = [UIBezierPath bezierPath];
_bezierPath.lineWidth = 2;
_bezierPath.lineCapStyle = kCGLineCapRound;
_bezierPath.lineJoinStyle = kCGLineCapRound;
}
return _bezierPath;
}
- (void)drawRect:(CGRect)rect {
//重绘之前移除所有point
[self.bezierPath removeAllPoints];
if (self.pointArray.count == 3) {
[self.bezierPath moveToPoint:self.pointArray[0].center];
//终止点、控制点
[self.bezierPath addQuadCurveToPoint:self.pointArray[2].center controlPoint:self.pointArray[1].center];
[self.bezierPath stroke];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.pointArray.count<3) {
UIView *pointview = [self createViewAtPoint:[[touches anyObject]locationInView:self]];
[self.pointArray addObject:pointview];
[self addSubview:pointview];

}else{
    UIView *view = self.pointArray[1];
    view.center = [[touches anyObject]locationInView:self];
    [self setNeedsDisplay];
}
}

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

if (self.pointArray.count == 3) {
    UIView *view = self.pointArray[1];
    view.center = [[touches anyObject]locationInView:self];
    [self setNeedsDisplay];
}
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.pointArray.count == 3) {
    UIView *view = self.pointArray[1];
    view.center = self.pointArray[2].center;
    [self setNeedsDisplay];
}
}
-(UIView *)createViewAtPoint:(CGPoint)point
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(point.x-5, point.y-5, 10, 10)];
view.layer.cornerRadius = 5;
view.backgroundColor = [UIColor blueColor];
return view;
}
@end

只给了一个controlPoint是可变的,起始点和终止点写死。drawRect之前清空曲线上的points

上一篇下一篇

猜你喜欢

热点阅读