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