2014-02-07 23 views
1

可拖動的UIView我米的iOS初學者和我已經建立以下層次結構,用的UITextView

1. uiview 

    2. uiview 

     3. uitextview 

UITextView的和的UIView的大小(2)是相同的。

現在的問題是,我想拖uitextview拖動uiview ... ???

我曾嘗試以下,但它不能爲我的作品..

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
     UITouch *aTouch = [touches anyObject]; 
     CGPoint location = [aTouch locationInView:self.noteTextView]; 
     [UIView beginAnimations:@"Dragging A DraggableView" context:nil]; 
     self.frame = CGRectMake(location.x, location.y, self.frame.size.width, self.frame.size.height); 
     [UIView commitAnimations]; 
} 

這裏noteTextView是的UITextView的對象..

+0

Y你不使用滾動視圖? – LML

+0

不,我不能使用scrollview,bcoz我想uiview像小note.o它不會包含太多的數據.. !!! –

回答

1

要拖動您可以使用下面的代碼視圖(from ray's tutorial ):

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer { 

    CGPoint translation = [recognizer translationInView:self.view]; 
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
            recognizer.view.center.y + translation.y); 
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; 

} 

記得在視圖上啓用用戶交互。

另外,不知道你在做什麼,但它聽起來像你想使用標籤而不是UITextView。

+0

No ..我想創建一個包含文本以及圖像的註釋,現在我試圖只用於文本,所以我已經在uiview(2)上添加了uitextview,稍後我將添加uiimageview來放置圖像...和uiview(1)就像所有這些小型uiviews(2)的容器一樣...... –

+1

沒錯。所以,如果你想要移動,你需要移動視圖而不是文本,你可以使用我給你的代碼。 – Mika

+0

而不是self.view我用self.noteTextView和它的移動uitextview,我想移動整個uiview(2)當你拖動uitextview ... –