我有一个包含图像视图的滚动视图。我向该图像添加了一些UIview,我想将它们拖放到图像区域。
我已经使用UIPanGestureRecognizer(Draggable UIView Swift 3) 但这不起作用。
@IBOutlet weak var mapImage: UIImageView!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
let viewDemo = UIView()
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 6.0
let panGesture = UIPanGestureRecognizer(target: self, action:
#selector(moveObject(_:)))
viewDemo.frame = CGRect(x: 50, y: 50, width: 40, height: 40)
viewDemo.backgroundColor = .red
viewDemo.layer.cornerRadius = 20
viewDemo.isUserInteractionEnabled = true
viewDemo.addGestureRecognizer(panGesture)
mapImage.addSubview(viewDemo)
}
@objc func moveObject(_ gesture: UIPanGestureRecognizer){
//1. Check That We Have A Valid Draggable View
guard let draggedObject = gesture.view else { return }
if gesture.state == .began || gesture.state == .changed {
//2. Set The Translation & Move The View
let translation = gesture.translation(in: self.view)
draggedObject.center = CGPoint(x: draggedObject.center.x + translation.x, y: draggedObject.center.y + translation.y)
gesture.setTranslation(CGPoint.zero, in: self.view)
}else if gesture.state == .ended {
}
}
当我拖放UIview时,moveObject
无法执行。
我已经尝试使用scrollView?.isScrollEnabled = false
答案 0 :(得分:2)
您必须启用UIImageView
的用户交互。
mapImage.isUserInteractionEnabled = true