【发布时间】:2016-11-18 21:41:13
【问题描述】:
在 UIViewController 的主视图中,我有一个 mapView 和另一个位于 mapView 上方的视图(假设是视图 A)。它们都具有等于 self.view.bounds 的帧。视图 A 是一个可调整大小的矩形,类似于用于裁剪图像的矩形。我的目标是让用户在地图上指定一个区域。因此,我希望用户能够放大地图并更改矩形的宽度和高度比例,因为仅让视图 A 成为无法实现的正方形会限制太多。
我从 GitHub https://github.com/justwudi/WDImagePicker 获得了这个项目,我从中使用了可调整大小的矩形功能。在 Github 链接的第二张图片中,有一个带有 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) 不起作用。调用 return 会导致崩溃并且效果不佳。这个问题有解决办法吗?
感谢您的时间和考虑。
【问题讨论】:
标签: ios iphone swift uigesturerecognizer uitouch