在滚动视图中拖动 uiview
Posted
技术标签:
【中文标题】在滚动视图中拖动 uiview【英文标题】:Dragging a uiview in a scrollview 【发布时间】:2019-04-15 08:24:44 【问题描述】:我有一个包含图像视图的滚动视图。我向该图像添加了一些 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
禁用滚动滚动视图
【问题讨论】:
【参考方案1】:您必须启用UIImageView
的用户交互。
mapImage.isUserInteractionEnabled = true
【讨论】:
以上是关于在滚动视图中拖动 uiview的主要内容,如果未能解决你的问题,请参考以下文章