Quartz2D _簡單畫板

#import "DrawView.h"

@interface DrawView()
//保存獲取到的當(dāng)前路徑
@property (strong, nonatomic) UIBezierPath *path;
//保存所有路徑(遍歷繪制)
@property (strong, nonatomic) NSMutableArray *paths;

@end

@implementation DrawView

- (NSMutableArray *)paths{
    
    if (_paths == nil) {
        
        _paths = [NSMutableArray array];
    }
    
    return _paths;
}

//重繪: 就是指把之前的路徑全部清除到, 在重新畫, 想要獲取之前路徑, 建立一個數(shù)組來保存之前的路徑, 再遍歷數(shù)組,
- (void)drawRect:(CGRect)rect {
    for (UIBezierPath *path in self.paths) {
        
        //畫圖
        [path stroke];
    }
}

//起點方法
//給定觸摸點
- (CGPoint)poinwiTouches:(NSSet *)touches{
    
    //1. 獲取到觸摸對象, 觸摸的當(dāng)前坐標(biāo)
    UITouch *touch = [touches anyObject];
    
    return [touch locationInView:self];
}

//觸摸到哪, 就從哪畫
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //1. 創(chuàng)建路徑
    self.path = [UIBezierPath bezierPath];
    
    //2. 獲取到觸摸的點
    CGPoint point = [self poinwiTouches:touches];
    
    //3. 確定起點在哪
    [self.path moveToPoint:point];

    //4. 獲取開始路徑(創(chuàng)建一個路徑, 就保存到數(shù)組)
    [self.paths addObject:self.path];
    
}

//手指移動畫線(只要繪制不停移動, 就會一直走該方法, 不斷創(chuàng)建結(jié)束點)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //獲取觸摸點
    CGPoint point = [self poinwiTouches:touches];
    
    //確定終點
    //添加一條線
    [self.path addLineToPoint:point];
    
    //重新繪制(只要移動位置, 就不停重新調(diào)用drawRect方法, 實現(xiàn)實時的更新繪制路徑  )
    [self setNeedsDisplay];
    //重繪: 就是指把之前的路徑全部清除到, 在重新畫, 想要獲取之前路徑, 建立一個數(shù)組來保存之前的路徑, 再遍歷數(shù)組,
   
}```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容