ios - 可拖动的 UITextView 没有响应

标签 ios swift uitextview draggable

我创建了一个需要可拖动的自定义 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 成为第一响应者。

关于ios - 可拖动的 UITextView 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025687/

相关文章:

IOS 9 不请求远程通知权限

ios - Swift 3 - 从另一个类中的 XIB 实例访问变量

swift - swift addMenuItem 的语法是什么

ios - 将 UITextView 中的文本替换为可链接文本 - iOS

ios - 保存 api Facebook 数据以在不同的 ViewController swift iOS 中使用

ios - 非系统字体不适用于 iOS6

ios - UITextField 和键盘通知 - 奇怪的顺序

ios - 快速限制在 UITextView 中复制和粘贴的字符限制?

iphone - 如何下载和设置不在主线程上的 UIImage

ios - 如何知道用户是否共享了我的应用程序?