首先推薦一下一直在看的博客文頂頂?shù)牟┛?/a>基本上涵蓋了iOS入門到中高級(jí)的所有知識(shí)點(diǎn),而且極度詳細(xì)含潘。是的饲做,極度!遏弱!
如果老爺們有哪里忘了盡管去找盆均,包你找到。
貝塞爾曲線漱逸,其實(shí)了解的很皮毛泪姨,所以記錄一下總結(jié)一下。
概念我就不寫了饰抒,怕自己理解的有偏頗
一肮砾、創(chuàng)建
+ (instancetype)bezierPath;
+ (instancetype)bezierPathWithRect:(CGRect)rect;//矩形
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;//內(nèi)切圓或者橢圓
+ (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;//根據(jù)路徑劃線
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
二循集、Talk is cheap!Show you my code!
1.創(chuàng)建繼承UIView的子類
2.重寫drawRect: 方法(創(chuàng)建路徑-繪圖)
#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];
//終止點(diǎn)唇敞、控制點(diǎn)
[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
只給了一個(gè)controlPoint是可變的蔗草,起始點(diǎn)和終止點(diǎn)寫死咒彤。drawRect之前清空曲線上的points