Swift无法编辑UITextView并且无法添加文本

时间:2016-07-17 03:47:53

标签: swift

我正在尝试将UITextView添加到我的视图中。我有这门课:

import UIKit

class TextAnnotation: UITextView {

    var lastLocation:CGPoint?
    var panRecognizer:UIPanGestureRecognizer?

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        self.panRecognizer = UIPanGestureRecognizer(target:self, action: #selector(StampAnnotation.detectPan))
        self.gestureRecognizers = [panRecognizer!]
        self.text = "Text Field"
        self.backgroundColor = UIColor.brownColor()
        self.userInteractionEnabled = true
        self.editable = true
        self.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: 200, height: 50)

    }

    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

    }

}

背景颜色将变为棕色,但文本没有添加,当我点击UITextView时,当我设置为可编辑时没有任何反应,我做错了什么?

以下是我如何称呼这个课程:

let textCenter = touches.first!.locationInView(view)
            let textView = TextAnnotation(frame: CGRect(x: textCenter.x, y: textCenter.y, width: 200, height: 50), textContainer: NSTextContainer())

            view.addSubview(textView)

1 个答案:

答案 0 :(得分:2)

你是那个肆无忌惮地剥离了这个文本视图的真实手势识别器并用其他东西替换它们的人,有效地削弱了文本视图。不要这样做。不要弄乱文本视图的手势识别器!如果启用了交互且可编辑为true,则此文本视图需要能够自行完成工作。