3
사용자가 드래그하거나 터치하는 동안 선이 보이도록 두 손가락으로 직선을 그리거나 그릴 수있는 방법은 무엇입니까? 난 이미 coregraphics를 사용하여 간단한 그림을 시도했지만이 하나가 나를 위해 복잡하게 보인다.두 손가락으로 직선을 그립니다.
사용자가 드래그하거나 터치하는 동안 선이 보이도록 두 손가락으로 직선을 그리거나 그릴 수있는 방법은 무엇입니까? 난 이미 coregraphics를 사용하여 간단한 그림을 시도했지만이 하나가 나를 위해 복잡하게 보인다.두 손가락으로 직선을 그립니다.
Justin의 말에 따르면 touchesBegan
과 touchesMoved
만 처리하면됩니다. 당신이 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
, 당신은필요.
touchesBegan에서 트랙이 터치되고 touchesMoved가 터치됩니다. CoreGraphics를 사용하여 DrawRect 뷰에 그립니다. –
내 견해에서 접촉을 추적 할 수 있습니다. 질문은 두 손가락으로 어떻게됩니까? 다른 손가락의 현재 위치 (x 및 y)를 어떻게 감지합니까? 선을 그릴 위치를 식별하기 위해 1 및 2 번째 손가락의 x 및 y를 모두 가져와야하기 때문에. –
@ jeraldov : 각 터치는 손가락에 해당합니다. –