可拖动的 UITextView 没有响应

Draggable UITextView Not Responding

我创建了一个需要可拖动的自定义 UITextView,这是我使用的代码:

class DraggableView: UITextView {

var lastLocation:CGPoint = CGPointMake(0, 0)

override init(frame: CGRect, textContainer: NSTextContainer?) {
    super.init(frame: frame, textContainer: textContainer)
    // Initialization code
    var panRecognizer = UIPanGestureRecognizer(target:self, action:"detectPan:")
    self.gestureRecognizers = [panRecognizer]

    self.editable = true
    self.selectable = true
    self.backgroundColor = UIColor.redColor()
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func detectPan(recognizer:UIPanGestureRecognizer) {
    var translation  = recognizer.translationInView(self.superview!)
    self.center = CGPointMake(lastLocation.x + translation.x, lastLocation.y + translation.y)
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    // Promote the touched view
    //self.superview?.bringSubviewToFront(self)

    // Remember original location
    lastLocation = self.center
}
}

出现了,但问题是我不能写任何东西或与之交互,我只能移动它。

问题是什么?

我认为你应该替换代码

self.gestureRecognizers = [panRecognizer]

self.addGestureRecognizer(panRecognizer)

我认为必须有一个属于 gestureRecognizers 的手势告诉 textView 成为第一响应者。