缩放后无法平移(移动)UIView

时间:2019-04-03 09:47:05

标签: ios swift

缩放视图过多后,无法平移(移动)UIView。视图将停止以一定比例响应平移。因此,如果我要使用平底锅,则必须缩小,然后使用平底锅(移动)。

我已经搜索过,但大多数查询与UIScrollView有关。我还没有找到UIView的解决方案。

@objc func pan(sender : Any){
        if startCrooping == true  || isBackgroundMood == true { // &&
            if let sender = sender as? UIPanGestureRecognizer {
                //the ContainerForBackgroundAndForegroundImage is UIview
                ContainerForBackgroundAndForegroundImage.isUserInteractionEnabled = true

                var newCenter = sender.translation(in: self.view)
                if(sender.state == .began){
                    beginX = ContainerForBackgroundAndForegroundImage.center.x
                    beginY = ContainerForBackgroundAndForegroundImage.center.y
                }
                newCenter = CGPoint.init(x: beginX + newCenter.x, y: beginY + newCenter.y)
                ContainerForBackgroundAndForegroundImage.center = newCenter
                }
        }
}



这是捏码

@objc func scale(sender : Any){
    if startCrooping == true || isBackgroundMood == true{
        let sender = sender as? UIPinchGestureRecognizer
        if sender!.state == .began {
            isPinching = true

            let currentScale = self.ContainerForBackgroundAndForegroundImage.frame.size.width / self.ContainerForBackgroundAndForegroundImage.bounds.size.width
            var newScale = currentScale*sender!.scale
        }
        else if sender?.state == .changed{

            //point to the exact pinch point
            guard let view = sender!.view else {return}
            let pinchCenter = CGPoint(x: sender!.location(in: view).x - view.bounds.midX,
                                      y: sender!.location(in: view).y - view.bounds.midY)
            let transform = view.transform.translatedBy(x: pinchCenter.x, y: pinchCenter.y)
                .scaledBy(x: sender!.scale, y: sender!.scale)
                .translatedBy(x: -pinchCenter.x, y: -pinchCenter.y)
            let currentScale = self.ContainerForBackgroundAndForegroundImage.frame.size.width / self.ContainerForBackgroundAndForegroundImage.bounds.size.width
            var newScale = currentScale*sender!.scale

            // scale
            if newScale < 1 {
                print("pinching now new scale less than 1")
                self.ContainerForBackgroundAndForegroundImage.transform = transform


                newScale = 1.0

                sender!.scale = 1.0
            } else {
                print("pinching now larger than 1")
                ContainerForBackgroundAndForegroundImage.transform = transform

                sender!.scale = 1.0

            }
        }
    }
}

因此,使用此代码,我可以放大/缩小和平移,但是如果放大得太多,我将无法平移。有时,我无法缩小。

0 个答案:

没有答案