iOSのベッセル曲線の作成とcontrolPointの曲線角度の動的調整には、小さな描画ソフトが付属しています.

3281 ワード

まず、ずっと見ていたブログのトップのブログをお勧めします.基本的に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を継承するサブクラスを作成する.書き換えdrawRect:メソッド(作成パス-図面)#import"PaintView.h"@interface PaintView()@property(nonatomic,strong)NSMutableArray*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 PatadQuadCurveToPoint:self.pointArray[2].c enter controlPoint:self.pointintintpointintintArray[2].c enter controlPoint:self.pointpointintintpointintintintintintpointintArray[2].cener contrArray[1].center;[self.bezierPath stroke];}-(void)touchesBegan:(NSSet *)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 *)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 *)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

コントロールポイントを1つだけ与えたのは可変で、開始点と終了点は死んでいます.drawRectの前に曲線上のpointsをクリア