【问题标题】:Draggable UITextView可拖动的 UITextView
【发布时间】:2015-11-08 14:22:05
【问题描述】:

我需要创建一个可拖动的 UITextView。 我设法创建了一个可拖动的视图,并尝试将 TextView 添加为子视图,但它不起作用,TextView 不可编辑,它没有出现在视图内而是出现在它的下方。

这是我创建的自定义视图代码:

class DraggableView: UIView {

var lastLocation:CGPoint = CGPointMake(0, 0)

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

    self.backgroundColor = UIColor.redColor()

    //add UITextView
    var text = UITextView()
    text.backgroundColor = UIColor.brownColor()
    text.text = "This is a test"
    text.frame = CGRectMake(self.center.x, self.center.y, 40, 40)
    self.addSubview(text)
}

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
}
}    

如何正确地将 TextView 添加到可拖动视图?

【问题讨论】:

    标签: ios swift uitextview draggable


    【解决方案1】:

    试试这个代码,我更新了你的detectPan方法,你可能需要根据你的要求进行调整

    class DraggableView: UIView {
    
    var lastLocation:CGPoint = CGPointMake(0, 0)
    var startFrame:CGRect!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // Initialization code
        var panRecognizer = UIPanGestureRecognizer(target:self, action:"detectPan:")
        self.gestureRecognizers = [panRecognizer]
    
        self.backgroundColor = UIColor.redColor()
    
        //add UITextView
        var text = UITextView()
        text.backgroundColor = UIColor.brownColor()
        text.text = "This is a test"
        text.frame = CGRectMake(self.center.x, self.center.y, 40, 40)
        self.addSubview(text)
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func detectPan(gesture:UIPanGestureRecognizer) {
        switch(gesture.state)
        {
            case .Began:
                self.startFrame = gesture.view?.frame
            case .Changed:
                let newCoord = gesture.locationInView(gesture.view)
                let dX = newCoord.x
                let dY = newCoord.y
                let center = CGPointMake((gesture.view?.frame.origin.x)! + dX, (gesture.view?.frame.origin.y)! + dY)
                gesture.view?.center = center
            case .Ended:
                gesture.view?.frame = self.startFrame
                print("ended")
            default:
                print("will not handel")
    
        }
    }
    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        // Promote the touched view
        //self.superview?.bringSubviewToFront(self)
    
        // Remember original location
        lastLocation = self.center
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      相关资源
      最近更新 更多