ios畫畫板

來源:互聯網
上載者:User

標籤:

  1 @interface NJView ()  2 /**  3  *  定義一個大數組(大數組中儲存小數組, 每一個小數組儲存一條直線所有的點)  4  */  5 @property (nonatomic, strong) NSMutableArray *totalPoints;  6   7 @end  8   9 @implementation NJView 10  11 - (NSMutableArray *)totalPoints 12 { 13     if (_totalPoints == nil) { 14         _totalPoints = [NSMutableArray array]; 15     } 16     return _totalPoints; 17 } 18  19 // 開始觸摸 20 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 21 { 22 //    NSLog(@"touchesBegan"); 23      24     // 1.擷取手指對應UITouch對象 25     UITouch *touch = [touches anyObject]; 26     // 2.通過UITouch對象擷取手指觸摸的位置 27     CGPoint startPoint = [touch locationInView:touch.view]; 28      29     // 3.將手指觸摸的起點儲存到數組中 30     // [self.points addObject:[NSValue valueWithCGPoint:startPoint]]; 31      32     // 3.建立一個小數組,用於儲存當前路徑所有的點 33     NSMutableArray *subPoints = [NSMutableArray array]; 34     // 4.將手指觸摸的起點儲存到小數組中 35     [subPoints addObject:[NSValue valueWithCGPoint:startPoint]]; 36     // 5.將小數組儲存到大數組中 37     [self.totalPoints addObject:subPoints]; 38  39 } 40 // 移動 41 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 42 { 43     // 1.擷取手指對應UITouch對象 44     UITouch *touch = [touches anyObject]; 45     // 2.通過UITouch對象擷取手指觸摸的位置 46     CGPoint movePoint = [touch locationInView:touch.view]; 47     // 3.將手指移動時觸摸的點儲存到數組中 48     // [self.points addObject:[NSValue valueWithCGPoint:movePoint]];‘ 49     // 4.從大數組中取出當前路徑對應的小數組 50     NSMutableArray *subPoints = [self.totalPoints lastObject]; 51     // 5.將手指移動時觸摸的點儲存到小數組中 52     [subPoints addObject:[NSValue valueWithCGPoint:movePoint]]; 53      54      55     // 6.調用drawRect方法重回視圖 56     [self setNeedsDisplay]; 57  58 } 59  60 // 離開view(停止觸摸) 61 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 62 { 63     // 1.擷取手指對應UITouch對象 64     UITouch *touch = [touches anyObject]; 65     // 2.通過UITouch對象擷取手指觸摸的位置 66     CGPoint endPoint = [touch locationInView:touch.view]; 67  68     // 3.將手指離開時觸摸的點儲存到數組中 69     // [self.points addObject:[NSValue valueWithCGPoint:endPoint]]; 70      71     // 4.從大數組中取出當前路徑對應的小數組 72     NSMutableArray *subPoints = [self.totalPoints lastObject]; 73     // 5.將手指移動時觸摸的點儲存到小數組中 74     [subPoints addObject:[NSValue valueWithCGPoint:endPoint]]; 75      76     // 6.調用drawRect方法重回視圖 77     [self setNeedsDisplay]; 78  79 } 80  81 // 畫線 82 - (void)drawRect:(CGRect)rect 83 { 84     85     // 1.擷取上下文 86     CGContextRef ctx = UIGraphicsGetCurrentContext(); 87     // 遍曆大數組,取出所有的小數組(每一個小數組代表一條線段) 88     for (NSMutableArray *subPointArray in self.totalPoints) { 89         // 遍曆小數組, 取出小數組中所有的點 90         for (int index = 0; index < subPointArray.count; index++) { 91             // 1.取出小數組中的每一個點 92              CGPoint point = [subPointArray[index] CGPointValue]; 93             // 2.繪製線段 94             if (0 == index) { 95                 // 2.1. 設定線段的起點 96                 CGContextMoveToPoint(ctx, point.x, point.y); 97             }else 98             { 99                 // 2.2.設定線段的終點100                 CGContextAddLineToPoint(ctx, point.x, point.y);101             }102         }103     }104     105     /*106     for (int index = 0; index < self.points.count; index++) {107         CGPoint point = [self.points[index] CGPointValue];108         // 2.繪製線段109         if (0 == index) {110             // 2.1. 設定線段的起點111             CGContextMoveToPoint(ctx, point.x, point.y);112         }else113         {114             // 2.2.設定線段的終點115             CGContextAddLineToPoint(ctx, point.x, point.y);116         }117 118     }119      */120     121     CGContextSetLineCap(ctx, kCGLineCapRound);122     CGContextSetLineJoin(ctx, kCGLineJoinRound);123     CGContextSetLineWidth(ctx, 10);124     125     // 3.渲染126     CGContextStrokePath(ctx);127     128 }129 130 - (void)clearView131 {132     [self.totalPoints removeAllObjects];133     [self setNeedsDisplay];134 }135 - (void)backView136 {137     [self.totalPoints removeLastObject];138     [self setNeedsDisplay];139 }

 

ios畫畫板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.