【问题标题】:Draggable UITextView Not Responding可拖动的 UITextView 没有响应
【发布时间】:2015-11-08 15:10:12
【问题描述】:

我已经创建了一个需要可拖动的自定义 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
}
}

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

有什么问题?

【问题讨论】:

  • 完成这项工作绝对不是那么简单,我在 ObjC 中有答案,但我没有时间翻译它,但它比你那里的内容要复杂得多。跨度>

标签: ios swift uitextview draggable


【解决方案1】:

我认为你应该替换代码

self.gestureRecognizers = [panRecognizer]

self.addGestureRecognizer(panRecognizer)

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

【讨论】:

  • 非常感谢!解决了问题
猜你喜欢
  • 2015-11-08
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 2013-07-15
  • 1970-01-01
  • 2015-12-21
  • 1970-01-01
  • 2018-12-11
相关资源
最近更新 更多