2015-08-15 23 views
1

我已經創建了需要可拖動一個自定義的UITextView的,這是我的代碼使用方法:可拖動的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 
} 
} 

的顯示出來,但問題是,我不能寫任何東西,或與之互動它,我只能移動它。

什麼問題?

+0

做這件事絕對不是那麼簡單,我在ObjC有答案,但我沒有時間翻譯它,但它比你在那裏有更多的參與。 – Loxx

回答

0

我想你應該

self.addGestureRecognizer(panRecognizer) 

替換代碼

self.gestureRecognizers = [panRecognizer] 

我認爲必須有whick屬於gestureRecognizers告訴TextView的成爲第一個響應者的姿態。

+0

非常感謝!解決了這個問題 – OmerN