iOS在屏幕上拖拽图片
2018-12-10 17:01 法子 阅读(1098) 评论(0) 编辑 收藏 举报override func viewDidLoad() { super.viewDidLoad() addPanGesture() } fileprivate func addPanGesture() { let tap = UIPanGestureRecognizer(target: self, action: #selector(dragOnView(_:))) draggedView.isUserInteractionEnabled = true draggedView.addGestureRecognizer(tap) } @objc fileprivate func dragOnView(_ drag: UIPanGestureRecognizer) { if drag.state == .began || drag.state == .changed { //手指移动了多少距离 var newPosition = drag.translation(in: drag.view) //视图应该移动到哪个位置 newPosition.x += drag.view!.center.x newPosition.y += drag.view!.center.y drag.view?.center = newPosition //将视图的位置设为新位置 drag.setTranslation(CGPoint.zero, in: drag.view) } }