【发布时间】: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