分享一个iOS下实现基本绘画板功能的简单方法

2020-01-14 16:46:12王旭

    for (int j = 0 ; j < [self.points_all count]; j++) {  
        NSMutableArray *points_tmp = [points_all objectAtIndex:j];  
              
            for (int i = 0;i < [points_tmp count]-1;i++)  
            {  
                CGPoint point1 = [[points_tmp objectAtIndex:i] CGPointValue];  
                CGPoint point2 = [[points_tmp objectAtIndex:(i+1)] CGPointValue];  
                CGContextMoveToPoint(context, point1.x, point1.y);  
                CGContextAddLineToPoint(context, point2.x, point2.y);  
                CGContextStrokePath(context);  
            }  
        }  
      
    //画这次  
    for (int i=0; i < [self.points count]-1; i++) {  
        CGPoint point1 = [[self.points objectAtIndex:i] CGPointValue];  
        CGPoint point2 = [[self.points objectAtIndex:(i+1)] CGPointValue];  
        CGContextMoveToPoint(context, point1.x, point1.y);  
        CGContextAddLineToPoint(context, point2.x, point2.y);  
        CGContextStrokePath(context);  
    }      
}  
  
//不支持多点触摸  
- (BOOL) isMultipleTouchEnabled  
{  
    return NO;  
}  
  
//创建一个array,并且记录初始ponit  
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event  
{  
    self.points = [NSMutableArray array];  
    CGPoint pt = [[touches anyObject] locationInView:self];  
    [self.points addObject:[NSValue valueWithCGPoint:pt]];  
}