在UIViewController的主视图中,我有一个mapView和另一个视图(比如视图A),它位于mapView之上。它们的帧都等于self.view.bounds。视图A是一个可调整大小的矩形,类似于用于裁剪图像的矩形。我的目标是让用户在地图上指定一个区域。因此,我希望用户能够缩放出地图,以及改变矩形的宽度和高度比例,因为只有让视图A是一个无法实现的方块会限制它太多。
我从GitHub https://github.com/justwudi/WDImagePicker获得了这个项目,我使用的是可调整大小的矩形功能。在第二张图片中,有一个有8个点和一个阴影区域的矩形。我希望能够让触摸传递到地图,这是后面的视图A,如果用户接触到阴影区域。只有当用户单击圆点内的区域或点(以便他想调整矩形的大小),我想让视图A识别触摸。因此,我修改了视图A上的代码,并有如下内容:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if cropBorderView.frame.contains(touch.location(in: self)){
print("touch contains - touchesbegan")
//self.isUserInteractionEnabled = true
}
else{
print("Touch does not contain - touchesbegan")
self.touchesCancelled(touches, with: event)
//return
}
let touchPoint = touch.location(in: cropBorderView)
anchor = self.calculateAnchorBorder(touchPoint)
fillMultiplyer()
resizingEnabled = true
startPoint = touch.location(in: self.superview)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
print("inside touches moved")
if let touch = touches.first {
if cropBorderView.frame.contains(touch.location(in: self)){
print("touch contains - touchesmoved")
//self.isUserInteractionEnabled = true
}
else{
print("Touch does not contain - touchesmoved ")
self.touchesCancelled(touches, with: event)
//return
}
if resizingEnabled! {
self.resizeWithTouchPoint(touch.location(in: self.superview))
}
}
}
它确实是识别触摸时,我点击内外,但它不是停止触摸,当我点击外部。这意味着调用self.touchesCancelled(touches, with: event)
不起作用。调用返回会导致崩溃,并且也不起作用。这个问题有什么解决办法吗?
感谢您的时间和考虑。
发布于 2016-11-18 23:12:58
touchesCancelled(_:with:)
只是UITouch
的一个通知,它不会以这种方式工作。据我所知,您在覆盖UIView
中实现了触摸处理程序,如果实现了,可以尝试从UIControl
类实现替换对self.touchesCancelled(touches, with: event)
的调用,使用cancelTracking(with:)
函数。
else {
print("Touch does not contain - touchesmoved ")
self.cancelTracking(with event)
}
基于
hitTest:
的更新解决方案
我已经检查了可能的解决方案,似乎您可以使用hitTest:
来避免不必要的触摸识别。下面的示例是Swift游乐场,您可以点击和拖动触摸并查看控制台中发生了什么:
import UIKit
import PlaygroundSupport
class JSGView : UIView {
var centerView = UIView()
override func didMoveToSuperview() {
frame = CGRect(x: 0, y: 0, width: 320, height: 480)
backgroundColor = UIColor.clear
centerView.frame = CGRect(x: 110, y: 190, width: 100, height: 100)
centerView.backgroundColor = UIColor.blue
addSubview(centerView)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dump(event)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if (hitTest(touch.location(in: self), with: event) != nil) {
print("Touch passed hit test and seems valid")
super.touchesCancelled(touches, with: event)
return
}
}
print("Touch isn't passed hit test and will be ignored")
super.touchesMoved(touches, with: event)
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if centerView.bounds.contains(centerView.convert(point, from: self)) {
return centerView
}
return nil
}
}
class JSGViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
let customView = JSGView()
view.addSubview(customView)
}
}
let controller = JSGViewController()
PlaygroundPage.current.liveView = controller.view
https://stackoverflow.com/questions/40686365
复制相似问题