gpt4 book ai didi

ios - 在同一个 UIImageView 上缩放和固定

转载 作者:行者123 更新时间:2023-11-28 07:28:53 25 4
gpt4 key购买 nike

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

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 来完成这两个部分吗?

最佳答案

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

原来 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
}
}
}

我使用了 UILongPressGestureRecognizerUITapGestureRecognizer 也能正常工作。

关于ios - 在同一个 UIImageView 上缩放和固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55633380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com