[swift]可拖动 UITextView 没有响应

标签: UITextView ios Swift
发布时间: 2015/12/26 3:08:53
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我已经创建了自定义的 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
}
}

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

问题是什么?

解决方法 1:

我认为你应该替换代码

self.gestureRecognizers = [panRecognizer]

self.addGestureRecognizer(panRecognizer)

必须有手势含铬属于 gestureRecognizers 告诉 textView,成为第一个响应程序?

官方微信
官方QQ群
31647020