如何在iOS中拖动某些图像?

想知道如何在屏幕上拖动一个图像,以及使用什么代码。 尝试查找,但只有旧版本的Swift有答案,不再工作。 我想拖动图像,但不要把手指放在屏幕上,它会去那个地方。 只要拖。

给我错误:

“使用未经申明的types'uitouch'”

import UIKit class DraggableImage: UIImageView { override func touchesMoved(touches: Set<uitouch>, withEvent event: UIEvent?) { if let touch = touches.first { let position = touch.locationInView(superview) center = CGPointMake(position.x, position.y) } } } 

你需要UIImageView ,在init中你需要设置userInteractionEnabled = true ,然后覆盖这个方法重写func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)好吧,我的代码是这样的:

  class DraggableImage: UIImageView { var localTouchPosition : CGPoint? required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.layer.borderWidth = 1 self.layer.borderColor = UIColor.red.cgColor self.isUserInteractionEnabled = true } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { let touch = touches.first self.localTouchPosition = touch?.preciseLocation(in: self) } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { super.touchesMoved(touches, with: event) let touch = touches.first guard let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{ return } self.frame.origin = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y) } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { self.localTouchPosition = nil } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code } */ } 

这是它的样子

在这里输入图像说明

希望这可以帮助

为移动视图创build一个Nsobject类并添加下面的代码

 import UIKit class objectClass: UIImageView, UIGestureRecognizerDelegate { /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { let touch: UITouch = touches.first! self.center = touch.location(in: self.superview) } } 

在mainViewController中创build一个NSobject类的对象

 var newView: objectClass = objectClass() 

点击button操作添加新的视图

 @IBAction func objectAdded(theButton: UIButton!) { let frame = CGRect(x: 100, y: 100, width: 44, height: 44) newView = objectClass(frame: frame) if theButton.titleLabel?.text == "image1" { newView.image = UIImage(named: "1") } else if theButton.titleLabel?.text == "image2" { newView.image = UIImage(named: "2") }else{ newView.image = UIImage(named: "3") } newView.contentMode = .scaleAspectFill newView.isUserInteractionEnabled = true self.view .addSubview(newView) newView.alpha = 0 UIView .animate(withDuration: 0.4) { self.newView.alpha = 1 } UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseOut, animations: { () -> Void in self.sliderViewBottomLayoutConstraint.constant = self.sliderViewBottomLayoutConstraint.constant - self.sliderViewBottomLayoutConstraint.constant self.view.layoutIfNeeded() }, completion: nil) image1Button.isEnabled = false image2Button.isEnabled = false image3Button.isEnabled = false let pinchGesture: UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.recognizePinchGesture(sender:))) pinchGesture.delegate = self let rotateGesture: UIRotationGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.recognizeRotateGesture(sender:))) let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.RemoveSelectedImageOnTap(sender:))) tapGesture.numberOfTapsRequired = 2 self.newView.addGestureRecognizer(tapGesture) self.newView.addGestureRecognizer(pinchGesture) self.newView.addGestureRecognizer(rotateGesture) } func recognizePinchGesture(sender: UIPinchGestureRecognizer) { sender.view!.transform = sender.view!.transform.scaledBy(x: sender.scale, y: sender.scale) sender.scale = 1 } func recognizeRotateGesture(sender: UIRotationGestureRecognizer) { sender.view!.transform = sender.view!.transform.rotated(by: sender.rotation) sender.rotation = 0 }