【发布时间】:2019-09-02 03:24:06
【问题描述】:
我在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,
zoom 有效,但 touchesEnded 函数未调用。 另一方面,如果 scrollView.userInteractionEnable = false touchesEnded 函数已调用但缩放无效。
touch.location(in:) 函数对我非常有用,因此我不想从UITouch 切换到UITapGestureRecognizer。
有没有办法使用UITouch 来完成这两个部分?
【问题讨论】:
-
我已经尝试过了,它正在工作(在第一个代码示例中)。我猜你没听懂这个问题。
标签: ios swift uiscrollview uiimageview pinchzoom