连接2点drawRect?

时间:2013-11-24 01:46:55

标签: ios objective-c cocoa-touch uiview drawrect

我正在尝试连接3分,具体取决于最后一次点击。例如,用户点击一次,它点一个点,用户再次点击,它从点击1绘制一条线到点击2.最后,用户再次点击,然后从点击2点击一条线到点击3.如果用户在点击时移动他的手指,它将不再绘制,只需移动创建的点。我试图开始只有2分,但它不会画,期间。这是我正在尝试的:

uiviewSubclass.h

@property (nonatomic) CGPoint firstPoint;
@property (nonatomic) CGPoint secondPoint;
@property (nonatomic) CGPoint thirdPoint;

uiviewSubclass.m

@implementation uiviewSubclass
{
    UIBezierPath *path;
    UIImage *incrementalImage; 
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setMultipleTouchEnabled:NO];
        [self setBackgroundColor:[UIColor clearColor]];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:5.0];

        self.firstPoint = CGPointZero;
        self.secondPoint = CGPointZero;
        self.thirdPoint = CGPointZero;

    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

        if(CGPointEqualToPoint(self.firstPoint, CGPointZero)){
            UITouch *touch = [touches anyObject];
            self.firstPoint = [touch locationInView:self];
            [path moveToPoint:self.firstPoint];
            self.hasSignature = @"YES";
        }
        else if(!(CGPointEqualToPoint(self.firstPoint, CGPointZero)) && CGPointEqualToPoint(self.secondPoint, CGPointZero)){
            UITouch *touch = [touches anyObject];
            self.secondPoint = [touch locationInView:self];
            [path moveToPoint:self.secondPoint];
            self.hasSignature = @"YES";
        }
    }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        if(CGPointEqualToPoint(self.firstPoint, CGPointZero)){
        UITouch *touch = [touches anyObject];
        self.firstPoint = [touch locationInView:self];
        [path moveToPoint:self.firstPoint];
        [self setNeedsDisplay];
        }
        else if(!(CGPointEqualToPoint(self.firstPoint, CGPointZero)) && CGPointEqualToPoint(self.secondPoint, CGPointZero)){
            UITouch *touch = [touches anyObject];
            self.secondPoint = [touch locationInView:self];
            [path addLineToPoint:self.secondPoint];
            [self setNeedsDisplay];
        }

    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *){
        [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    [incrementalImage drawInRect:rect]; // (3)
    [[UIColor redColor] setStroke];
    [path stroke];
}

为什么它不起作用的任何想法?我是drawRect的新手,我为任何初学者错误道歉。

2 个答案:

答案 0 :(得分:1)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        [drawImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"white" ofType:@"png"]]];
        return;
    }

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 20;

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    currentPoint.y -= 20;

    NSLog(@"current Point is x: %d, y: %d",currentPoint.x,currentPoint.y);

    UIGraphicsBeginImageContext(self.drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.drawImage.frame.size.width, self.drawImage.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 0.6, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}

答案 1 :(得分:1)

我建议您在视图中使用UITapGestureRecognizer。在识别每个点击时,更新每个点的路径。每次更新路径时都要调用setNeedsDisplay

您的drawRect:方法看起来是正确的。所以重要的是你的触摸逻辑来更新路径。