CGAffineTransformTranslate和CGAffineTransformScale之后的UIPoints不正确

时间:2013-12-06 22:48:30

标签: ios cgaffinetransform pinchzoom cgaffinetransformscale

我正在使用在Ray Wenderlich's教程中分享的想法使用UIKit开发简单的绘图应用程序。不同之处在于我需要实现一个功能,以便我可以缩放/缩放到我的绘图中并绘制更精细的线条。我可以使用CGAffineTransformScale(使用UIPinchGestureRecognizer)放大并使用CGAffineTransform在UIImage周围移动 - 问题是一旦放大了检测到的UITouch点并且实际触摸点有很大的偏移量。随着我不断缩放图像,这个偏移变大了。

在代码中

drawingImage - 用户与voluImage交互的一个 - 绘制的行被保存
transform_translate - CGAffinetransform
lastScale - CGFloat保存最后的缩放比例值
lastPoint - CGPoint保存最后触点
lastPointForPinch - CGPoint保存最后一个夹点

捏合手势在viewDidLoad中初始化为 -

pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)];
[self.drawingImage addGestureRecognizer:pinchGestureRecognizer];

UIPinchGesture检测的方法是 -

- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer
{

    if([recognizer state] == UIGestureRecognizerStateBegan) {
        // Reset the last scale, necessary if there are multiple objects with different scales

        lastScale = [recognizer scale];
        lastPointForPinch = [recognizer locationInView:self.drawingImage];

    }

    if ([recognizer state] == UIGestureRecognizerStateBegan ||
        [recognizer state] == UIGestureRecognizerStateChanged) {

        CGFloat currentScale = [[[recognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

        // Constants to adjust the max/min values of zoom
        const CGFloat kMaxScale = 2.0;
        const CGFloat kMinScale = 1.0;

        CGFloat newScale = 1 -  (lastScale - [recognizer scale]);
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);

        CGAffineTransform transform = CGAffineTransformScale([[recognizer view] transform], newScale, newScale);

        self.savingImage.transform = transform;
        self.drawingImage.transform=transform;

        lastScale = [recognizer scale];  // Store the previous scale factor for the next pinch gesture call

        CGPoint point = [recognizer locationInView:self.drawingImage];
        transform_translate = CGAffineTransformTranslate([[recognizer view] transform], point.x - lastPointForPinch.x, point.y - lastPointForPinch.y);

        self.savingImage.transform = transform_translate;
        self.drawingImage.transform=transform_translate;
        lastPointForPinch = [recognizer locationInView:self.drawingImage];

    }
}

绘制线条的方法(仅供参考,这是一个相当标准的程序,取自上面提到的教程,如果我在这里犯了一些错误就把它放在这里可以抓到) -

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.drawingImage];
    UIGraphicsBeginImageContext(self.savingImage.frame.size);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.drawingImage];
    CGContextRef ctxt = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctxt, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(ctxt, currentPoint.x, currentPoint.y);
    CGContextSetLineCap(ctxt, kCGLineCapRound);
    CGContextSetLineWidth(ctxt, brush );
    CGContextSetRGBStrokeColor(ctxt, red, green, blue, opacity);
    CGContextSetBlendMode(ctxt,kCGBlendModeNormal);
    CGContextSetShouldAntialias(ctxt,YES);
    CGContextSetAllowsAntialiasing(ctxt, YES);
    CGContextStrokePath(ctxt);   
    self.drawingImage.image = UIGraphicsGetImageFromCurrentImageContext();
    lastPoint = currentPoint;

}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if(!mouseSwiped) {
        UIGraphicsEndImageContext();
        UITouch *touch = [touches anyObject];
        CGPoint currentPoint = [touch locationInView:self.drawingImage];
        UIGraphicsBeginImageContext(self.drawingImage.frame.size);
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        self.drawingImage.image = UIGraphicsGetImageFromCurrentImageContext();
        [self.drawingImage.image drawInRect:CGRectMake(0, 0, self.drawingImage.frame.size.width, self.drawingImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];

    }
    UIGraphicsEndImageContext();
    UIGraphicsBeginImageContext(self.savingImage.frame.size);
    [self.savingImage.image drawInRect:CGRectMake(0, 0, self.savingImage.frame.size.width, self.savingImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.drawingImage.image drawInRect:CGRectMake(0, 0, self.drawingImage.frame.size.width, self.drawingImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    self.savingImage.image = UIGraphicsGetImageFromCurrentImageContext();
    self.drawingImage.image=nil;
    UIGraphicsEndImageContext();

    }
}

我尝试过CGPointApplyAffineTransform(指向,transform_translate),但仍然存在巨大的偏移。

希望我的问题得到清楚解释,有人可以帮助我。我一直在努力在这方面取得进展。提前致谢

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案......一次又一次地犯了一个愚蠢的错误。 locationInView需要来自self.view而不是来自图像。

@davidkonard感谢您的建议 - 实际上我没有意识到(在绘制应用程序的上下文中)用户触摸屏幕的意图恰好在那一点上绘图将完成,因此即使UIImageView仍被移动用户想在他的手指下绘制一个点/线/任何东西。所以locationInView应该是self.view(在我的情况下,self.view并没有被转换过。)

希望这能解释为什么我犯了一个错误以及我是如何想出解决方案的。