0

我正在开发一个 iOs 5 应用程序,您可以在其中在主视图的子视图内的 UIImageView 上进行绘制。当您绘制线条的前 30 像素时,不会跟随您的笔划。但真正罕见的是,如果您调整 ImageView 的大小并将其设置为所有屏幕 460x320 的大小(尽管您看不到完整的 ImageView,因为它比它所在的视图大。)它工作正常。所以也许有一个解决方案。那是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {   
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.drawImage];
    currentPoint.y -= 10; // only for 'kCGLineCapRound'
    UIGraphicsBeginImageContext(self.drawImage.frame.size);

    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5); // for size
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0); //values for R, G, B, and Alpha
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {      
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }
    if(!mouseSwiped) {
        //if color == green
        UIGraphicsBeginImageContext(self.drawImage.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

这就是我得到的

4

1 回答 1

1

问题是第一个touchesMoved:被 API 延迟了。我不确定是否有人能够解决这个问题。

请在https://feedbackassistant.apple.com/提交错误报告

不过,在您的情况下,真正的问题是一个简单的错误:您在 和 中使用了两个不同的坐标touchesBegan:touchesMoved:

接触开始:

lastPoint = [touch locationInView:self.view];
                                 //////////

感动:

CGPoint currentPoint = [touch locationInView:self.drawImage];
                                            ///////////////
于 2012-05-10T07:20:24.530 回答