使用uitextview拖拽uiview

时间:2014-02-07 11:40:33

标签: ios objective-c uiview

我是iOS初学者,我创建了以下层次结构,

 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的对象..

1 个答案:

答案 0 :(得分: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。