【问题标题】:Error painting on iOS app在 iOS 应用上绘制错误
【发布时间】:2012-05-09 21:00:53
【问题描述】:

我正在开发一个 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();
    }
}

【问题讨论】:

    标签: iphone objective-c ios5 drawing


    【解决方案1】:

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

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

    不过,在您的案例中,真正的问题是一个简单的错误:您在 touchesBegan:touchesMoved: 中使用了两个不同的坐标系。

    touchesBegan:

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

    touchesMoved:

    CGPoint currentPoint = [touch locationInView:self.drawImage];
                                                ///////////////
    

    【讨论】:

    • 对不起,这句话我说完了:)现在看,我想你会明白的!
    • @MartiSerraVivancos 请提交有关此行为的错误报告。如果没有足够的人抱怨,Apple 可能不会修复它。
    • 好的,我会做的。但是有什么方法可以在 iOs 应用中绘画/绘图?
    • 对不起,我不明白你的问题。根据您上面的示例,您 正在 已经在绘图。您必须找到一种方法来平滑前四分之一秒。
    • 因为您在第一季度第二次没有收到touchesMoved: 事件。提交错误报告!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2022-12-08
    相关资源
    最近更新 更多