4

我可以让捏/缩放功能像这样工作:

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

    if([recognizer state] == UIGestureRecognizerStateBegan) {
        _lastScale = 1.0;
    }

    CGFloat scale = 1.0 - (_lastScale - [recognizer scale]);

    CGAffineTransform currentTransform = self.imageForEditing.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [self.imageForEditing setTransform:newTransform];

    _lastScale = [recognizer scale];


}

问题是图像会根据其中心进行缩放,因此如果您首先平移图像使其不居中,然后捏合/缩放,它不会放大到触摸之间的区域。我认为这一定是一个已经解决和解决的常见问题,但我一直无法找到解决方案。

4

3 回答 3

3
@objc private func pinchHandler(gesture: UIPinchGestureRecognizer) {
    if let view = gesture.view {

        switch gesture.state {
        case .changed:
            let pinchCenter = CGPoint(x: gesture.location(in: view).x - view.bounds.midX,
                                      y: gesture.location(in: view).y - view.bounds.midY)
            let transform = view.transform.translatedBy(x: pinchCenter.x, y: pinchCenter.y)
                                            .scaledBy(x: gesture.scale, y: gesture.scale)
                                            .translatedBy(x: -pinchCenter.x, y: -pinchCenter.y)
            view.transform = transform
            gesture.scale = 1
        case .ended:
            // Nice animation to scale down when releasing the pinch.
            // OPTIONAL
            UIView.animate(withDuration: 0.2, animations: {
                view.transform = CGAffineTransform.identity
            })
        default:
            return
        }


    }
}
于 2017-05-09T13:39:14.973 回答
1

您可以将图层的锚点设置为两个捏触的“中心”:

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

    if([recognizer state] == UIGestureRecognizerStateBegan) {
        _lastScale = 1.0;
        if ([recognizer numberOfTouches] >= 2) { //should always be true when using a PinchGR
            CGPoint touch1 = [recognizer locationOfTouch:0 inView:self.imageForEditing];
            CGPoint touch2 = [recognizer locationOfTouch:1 inView:self.imageForEditing];
            CGPoint mid;
            mid.x = ((touch2.x - touch1.x) / 2) + touch1.x;
            mid.y = ((touch2.y - touch1.y) / 2) + touch1.y;
            CGSize imageViewSize = self.imageForEditing.frame.size;
            CGPoint anchor;
            anchor.x = mid.x / imageViewSize.width;
            anchor.y = mid.y / imageViewSize.height;
            self.imageForEditing.layer.anchorPoint = anchor;
        }
    }

    CGFloat scale = 1.0 - (_lastScale - [recognizer scale]);

    CGAffineTransform currentTransform = self.imageForEditing.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [self.imageForEditing setTransform:newTransform];

    _lastScale = [recognizer scale];


}

这样,图像应该相对于两次触摸之间的中心进行缩放。

有关更多详细信息,请参阅核心动画编程指南 - 核心动画基础

于 2013-04-20T11:27:32.120 回答
0

试试下面的代码来放大/缩小并使用捏手势识别器转换视图的中心。将这段代码放在你的 UIGestureRecognizerStateBegan 中:

        previousTouchPoint = [pinchRecognizer locationInView:self.view];

这在你的 UIGestureRecognizerStateChanged 中:

       //zoom in/out view based on scale
        CGFloat zoomScale = pinchRecognizer.scale;
        CGRect tRect = pinchedView.bounds;
        tRect.size.width = pinchedView.frame.size.width * zoomScale;
        tRect.size.height = pinchedView.frame.size.height * zoomScale;
        pinchedView.bounds = tRect;

        //sets the center of view
        CGPoint currentTouchPoint = [pinchRecognizer locationInView:self.view];
        CGPoint tCenter = pinchedView.center;
        tCenter.x -= (previousTouchPoint.x - currentTouchPoint.x);
        tCenter.y -= (previousTouchPoint.y - currentTouchPoint.y);
        pinchedView.center = tCenter;
        previousTouchPoint = [pinchRecognizer locationInView:self.view];
于 2013-12-06T05:53:56.257 回答