2013-04-22 33 views
3

如何用兩根手指在直線上繪製/繪製直線,同時拖動或觸摸仍然由用戶完成,線條可見?我已經使用coregraphics嘗試了簡單的繪畫,但是這對我來說似乎有點複雜。用兩根手指畫直線

+1

賽道觸動了一個觸動開始,並touchesMoved。使用CoreGraphics在視圖DrawRect中繪製它們。 – 2013-04-22 03:56:21

+0

我現在可以在我的視圖中跟蹤觸摸,問題是用2個手指如何檢測另一個手指的當前位置(x和y)?因爲我需要得到第一和第二手指的x和y來確定在哪裏畫線。 – 2013-04-22 06:26:39

+1

@jeraldov:每個觸摸對應一個手指。 – 2013-04-22 07:50:31

回答

3

對於賈斯汀的觀點,只要處理touchesBegantouchesMoved

因此,如果你繼承UIView,在CoreGraphics中實現可能是這樣的:

@interface CustomView() 

@property (nonatomic, strong) NSMutableArray *paths; 
@property (nonatomic, strong) UIBezierPath *currentPath; 

@end 

@implementation CustomView 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     [self setMultipleTouchEnabled:YES]; 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (!self.currentPath) 
    { 
     if (!self.paths) 
      self.paths = [NSMutableArray array]; 

     self.currentPath = [UIBezierPath bezierPath]; 
     self.currentPath.lineWidth = 3.0; 

     [self.paths addObject:self.currentPath]; 

     [self touchesMoved:touches withEvent:event]; 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if ([touches count] != 2) 
     return; 

    [self.currentPath removeAllPoints]; 

    __block NSInteger i = 0; 
    [touches enumerateObjectsUsingBlock:^(UITouch *touch, BOOL *stop) { 
     CGPoint location = [touch locationInView:self]; 

     if (i++ == 0) 
      [self.currentPath moveToPoint:location]; 
     else 
      [self.currentPath addLineToPoint:location]; 
    }]; 

    [self setNeedsDisplay]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.currentPath = nil; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    [[UIColor redColor] setStroke]; 
    [[UIColor clearColor] setFill]; 

    for (UIBezierPath *path in self.paths) 
    { 
     [path stroke]; 
    } 
} 

- (void)reset 
{ 
    self.paths = nil; 
    [self setNeedsDisplay]; 
} 

@end 

另外,您還可以使用Quartz 2D和定義自己的CAShapeLayer對象,然後無論你的看法子類,或者您的視圖控制器可以這樣做(不用說,這就是視圖控制器的實現;認爲實施應該是顯而易見的):

#import "ViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController() 

@property (nonatomic, weak) CAShapeLayer *currentLayer; 
@property (nonatomic, strong) UIBezierPath *currentPath; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self.view setMultipleTouchEnabled:YES]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (!self.currentLayer) 
    { 
     CAShapeLayer *layer = [CAShapeLayer layer]; 
     layer.lineWidth = 3.0; 
     layer.strokeColor = [[UIColor redColor] CGColor]; 
     layer.fillColor = [[UIColor clearColor] CGColor]; 
     [self.view.layer addSublayer:layer]; 
     self.currentLayer = layer; 

     self.currentPath = [UIBezierPath bezierPath]; 

     [self touchesMoved:touches withEvent:event]; 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if ([touches count] != 2) 
     return; 

    [self.currentPath removeAllPoints]; 

    __block NSInteger i = 0; 
    [touches enumerateObjectsUsingBlock:^(UITouch *touch, BOOL *stop) { 
     CGPoint location = [touch locationInView:self.view]; 

     if (i++ == 0) 
      [self.currentPath moveToPoint:location]; 
     else 
      [self.currentPath addLineToPoint:location]; 
    }]; 

    self.currentLayer.path = [self.currentPath CGPath]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.currentPath = nil; 
    self.currentLayer = nil; 
} 

- (IBAction)didTouchUpInsideClearButton:(id)sender 
{ 
    for (NSInteger i = [self.view.layer.sublayers count] - 1; i >= 0; i--) 
    { 
     if ([self.view.layer.sublayers[i] isKindOfClass:[CAShapeLayer class]]) 
      [self.view.layer.sublayers[i] removeFromSuperlayer]; 
    } 
} 

@end 

對於後一種方法,你需要add the QuartzCore.framework to your project