如何在视图屏幕上拖动UITextView?

时间:2010-07-12 19:46:10

标签: iphone objective-c iphone-sdk-3.0

我有一个项目,可以在屏幕上拖动UITextView(用于多行)。到目前为止,我对此的解决方案是隐藏UIButton的叠加,当拖动它的中心时,它与UITextView的中心相同。

但是我看到的应用程序似乎只是允许在运行中拖动和编辑UITextView,所以似乎可能没有覆盖在那些但我不确定。

思想?

顺便说一句,这段代码中的c是UIButton,这就是我迄今为止的移动方式:

- (void) draggedOut: (UIControl *) c withEvent: (UIEvent *) ev 
{

 if(self.interfaceOrientation == UIInterfaceOrientationPortrait)
 {
  c.center = [[[ev allTouches] anyObject] locationInView:self.view];
  AddedText.center = c.center;
 }
 else if(self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
 {
  c.center = [[[ev allTouches] anyObject] locationInView:self.view];
  AddedText.center = c.center;
 }
 else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
 {
  c.center = [[[ev allTouches] anyObject] locationInView:self.view];
  AddedText.center = c.center;
 }
 else if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
 {
  c.center = [[[ev allTouches] anyObject] locationInView:self.view];
  AddedText.center = c.center;
 }
}

3 个答案:

答案 0 :(得分:1)

- (void)panTextView:(UIPanGestureRecognizer *)recognizer {
      NSLog(@"panning");

 location1 = [recognizer translationInView:draggableTextView];


    recognizer.view.center = CGPointMake(recognizer.view.center.x + location1.x,
                                    recognizer.view.center.y + location1.y);



        [recognizer setTranslation:CGPointMake(0,0) inView:draggableTextView];

     location1 =[recognizer locationInView:draggableTextView];
       NSLog(@"tranlation %@",NSStringFromCGPoint(location1));

     [_imgpic addSubview:recognizer.view];

 appDelegate.txt=draggableTextView.text;
}

在创建textview后调用此方法。

答案 1 :(得分:0)

好吧无法操纵实际的uitextview。

首先尝试制作一个可以移动的按钮覆盖,然后按下该按钮以开始编辑,但它没有正确居中。

然后尝试了上面的方法来移动UITextView本身。但它只适用于触摸或拖拽。 (注意这是touchesBegan& touchesMoved的修改形式)

使用UITcrollView结束UITextView作为子视图。现在它可以平滑移动,只需它可以从屏幕上的任何位置移动。不是最佳的,但最好的结果是保持其他一切完整。

答案 2 :(得分:0)

textView是否需要支持滚动?如果是这样,这可能会变得复杂。

但如果没有,有两种方法。 1)子类textview并覆盖touchesBegan,touchesMoved,touchesEnded。 2)编写一个手势识别器,处理相同的消息并将其附加到textview。

以下是一个可以完成工作的手势识别器示例:

@interface TouchMoveGestureRecognizer : UIGestureRecognizer
{
    CGPoint _ptOffset;
}

@end

@implementation TouchMoveGestureRecognizer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* t = [touches anyObject];
    _ptOffset = [t locationInView: self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* t = [touches anyObject];
    CGPoint pt = [t locationInView: self.view.superview];
    pt.x -= _ptOffset.x;
    pt.y -= _ptOffset.y;

    CGRect r = self.view.frame;
    r.origin = pt;
    self.view.frame = r;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    _ptOffset = CGPointMake(-1, -1);
}

@end

以及如何使用它:

- (void)viewDidLoad {
    [super viewDidLoad];

    _textView.scrollEnabled = NO;

    TouchMoveGestureRecognizer* gr = [[[TouchMoveGestureRecognizer alloc] init] autorelease];
    [_textView addGestureRecognizer: gr];
}