2013-04-22 2 views
3

사용자가 드래그하거나 터치하는 동안 선이 보이도록 두 손가락으로 직선을 그리거나 그릴 수있는 방법은 무엇입니까? 난 이미 coregraphics를 사용하여 간단한 그림을 시도했지만이 하나가 나를 위해 복잡하게 보인다.두 손가락으로 직선을 그립니다.

+1

touchesBegan에서 트랙이 터치되고 touchesMoved가 터치됩니다. CoreGraphics를 사용하여 DrawRect 뷰에 그립니다. –

+0

내 견해에서 접촉을 추적 할 수 있습니다. 질문은 두 손가락으로 어떻게됩니까? 다른 손가락의 현재 위치 (x 및 y)를 어떻게 감지합니까? 선을 그릴 위치를 식별하기 위해 1 및 2 번째 손가락의 x 및 y를 모두 가져와야하기 때문에. –

+1

@ jeraldov : 각 터치는 손가락에 해당합니다. –

답변

3

Justin의 말에 따르면 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 

, 당신은 또한 다음 석영 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 

, 당신은필요.

관련 문제