缩放并固定在同一UIImageView上

时间:2019-04-11 13:14:58

标签: ios swift uiscrollview uiimageview pinchzoom

我在UIImageView中有一个UIScrollView,允许用户放大图像。这部分没问题。我为此使用以下代码:

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return self.imageView
}

此外,用户可以固定同一张图片中的某些位置。我使用以下代码固定图片,也没有问题。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: self.imageView)
    let touchedRect = CGRect(x: location.x - 10, y: location.y - 10, width: 20, height: 20)

    let touchedInCourt = touchedRect.intersects(self.imageView.bounds)
    switch touchedInCourt {
    case true:
        if self.imageView.subviews.count > 0 {
            var flagForIntersect = false
            for subview in self.imageView.subviews {
                let subviewFrame = subview.frame
                if subviewFrame.intersects(touchedRect) {
                    subview.removeFromSuperview()
                    flagForIntersect = true
                }
            }
            if !flagForIntersect {
                addDot(withLocation: location, toPhoto: self.imageView)
            }
        } else {
            addDot(withLocation: location, toPhoto: self.imageView)
        }
    case false: break
    }
}

func addDot(withLocation location: CGPoint, toPhoto photo: UIImageView) {
    let frame = CGRect(x: location.x - 15, y: location.y - 15, width: 30, height: 30)
    let tempImageView = UIImageView(frame: frame)
    tempImageView.image = UIImage(named: "Blue Dot")
    photo.addSubview(tempImageView)
}

在同一UIImageView中使用这两部分会导致问题。

如果scrollView.userInteractionEnable = true缩放有效,但touchesEnded函数未调用。另一方面,如果scrollView.userInteractionEnable = false touchesEnded函数已调用但缩放不起作用。

touch.location(in:)函数对我来说真的很有用,因此我不想从UITouch切换到UITapGestureRecognizer

有什么方法可以使用UITouch来完成这两部分吗?

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。

结果UITapGestureRecognizer也具有.location(in:)功能。

对于遇到相同问题的用户,我在下面添加了工作代码。

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
longPressRecognizer.minimumPressDuration = 0.5
longPressRecognizer.delaysTouchesBegan = true
longPressRecognizer.delegate = self
self.imageView.addGestureRecognizer(longPressRecognizer)

@objc func longPressed(sender: UILongPressGestureRecognizer) {
    if sender.state == UIGestureRecognizer.State.ended {
        let location = sender.location(in: self.imageView)
        let touchedRect = CGRect(x: location.x - 10, y: location.y - 10, width: 20, height: 20)

        let touchedInCourt = touchedRect.intersects(self.imageView.bounds)
        switch touchedInCourt {
        case true:
            if self.imageView.subviews.count > 0 {
                var flagForIntersect = false
                for subview in self.imageView.subviews {
                    let subviewFrame = subview.frame
                    if subviewFrame.intersects(touchedRect) {
                        subview.removeFromSuperview()
                        flagForIntersect = true
                    }
                }
                if !flagForIntersect {
                    addDot(withLocation: location, toPhoto: self.imageView)
                }
            } else {
                addDot(withLocation: location, toPhoto: self.imageView)
            }
        case false: break
        }
    }
}
  

我使用了UILongPressGestureRecognizer,但是UITapGestureRecognizer有效   也是