Swift: 实现可拖拽的UIImageView需要哪些方法?
Swift: which methods are needed to implement a draggable UIImageView?
如标题所述,我希望用户能够拖动用作图片图钉的 UIImageView,我已遵循 this 教程。这是我写的class:
import UIKit
class PinImageView: UIImageView {
var lastLocation:CGPoint?
var panRecognizer:UIPanGestureRecognizer?
init(imageIcon: UIImage?, location:CGPoint) {
super.init(image: imageIcon)
self.lastLocation = location
self.panRecognizer = UIPanGestureRecognizer(target:self, action:"detectPan:")
self.center = location
self.gestureRecognizers = [panRecognizer!]
self.frame = CGRect(x: location.x, y: location.y, width: 20.0, height: 30.0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func detectPan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.superview!)
self.center = CGPointMake(lastLocation!.x + translation.x, lastLocation!.y + translation.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Promote the touched view
self.superview?.bringSubviewToFront(self)
// Remember original location
lastLocation = self.center
}
}
那些引脚 UIImageViews
应该移动到更大的 UIImageView
上,它包含在 UIViewController
中:
class PhotoViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
var imagePicker: UIImagePickerController!
var redLocationA:PinImageView = PinImageView(imageIcon: UIImage(named: "pin1.png"), location: CGPointMake(80, 330))
var redLocationB:PinImageView = PinImageView(imageIcon: UIImage(named: "pin1.png"), location: CGPointMake(80, 360))
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(redLocationA)
self.view.addSubview(redLocationB)
}
...
}
当我启动应用程序时,一切都正确显示,但如果我尝试拖动图钉,它们不会移动。恐怕我错过了什么……你知道吗?
也许是方法 touchedìsMoved()
但教程中没有提到...
更新:回答
愚蠢的我,我错过了
self.userInteractionEnabled = true
在 init()
方法中,我认为它们是默认启用的。
如标题所述,我希望用户能够拖动用作图片图钉的 UIImageView,我已遵循 this 教程。这是我写的class:
import UIKit
class PinImageView: UIImageView {
var lastLocation:CGPoint?
var panRecognizer:UIPanGestureRecognizer?
init(imageIcon: UIImage?, location:CGPoint) {
super.init(image: imageIcon)
self.lastLocation = location
self.panRecognizer = UIPanGestureRecognizer(target:self, action:"detectPan:")
self.center = location
self.gestureRecognizers = [panRecognizer!]
self.frame = CGRect(x: location.x, y: location.y, width: 20.0, height: 30.0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func detectPan(recognizer:UIPanGestureRecognizer) {
let translation = recognizer.translationInView(self.superview!)
self.center = CGPointMake(lastLocation!.x + translation.x, lastLocation!.y + translation.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Promote the touched view
self.superview?.bringSubviewToFront(self)
// Remember original location
lastLocation = self.center
}
}
那些引脚 UIImageViews
应该移动到更大的 UIImageView
上,它包含在 UIViewController
中:
class PhotoViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
var imagePicker: UIImagePickerController!
var redLocationA:PinImageView = PinImageView(imageIcon: UIImage(named: "pin1.png"), location: CGPointMake(80, 330))
var redLocationB:PinImageView = PinImageView(imageIcon: UIImage(named: "pin1.png"), location: CGPointMake(80, 360))
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(redLocationA)
self.view.addSubview(redLocationB)
}
...
}
当我启动应用程序时,一切都正确显示,但如果我尝试拖动图钉,它们不会移动。恐怕我错过了什么……你知道吗?
也许是方法 touchedìsMoved()
但教程中没有提到...
更新:回答
愚蠢的我,我错过了
self.userInteractionEnabled = true
在 init()
方法中,我认为它们是默认启用的。