用两个手指画一条直线

3

我该如何使用两个手指画一条直线,并且在用户拖动或触摸时这条直线仍然可见?我已经尝试过使用核心图形进行简单绘画,但对我来说这似乎有些复杂。


1
在touchesBegan和touchesMoved中跟踪触摸。使用CoreGraphics在视图的drawRect中绘制它们。 - Justin Meiners
现在我可以在我的视图中跟踪触摸,问题是如何检测另一个手指的当前位置(x和y),当有两个手指时?因为我需要获取第一和第二个手指的x和y来确定画线的位置。 - SleepNot
1
@jeraldov:每个触摸对应一个手指。 - Peter Hosey
1个回答

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

另外,您还可以使用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

对于后一种方法,您需要将QuartzCore.framework添加到您的项目中


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接