开发者

How would I drag UITextView around the view screen?

I have a project where a UITextView (for multilines) can be dragged around 开发者_如何学Cthe screen. So far my solution to this has been an overlay of an invisible UIButton which when dragged its center is the same as the UITextView's center.

However I've seen apps that seem to just allow the UITextView to be dragged and edited on the fly so it seems there might not be an overlay in those but I'm not sure.

Thoughts?

By the way, c in this code is the UIButton and this is how I have moved it thus far:

- (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;
 }
}


- (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;
}

call this method after creating textview.


Well have not been able to manipulate the actual uitextview.

First tried making a button overlay that could be moved and could be pressed to start editing, but it wasn't centered properly.

Then tried the above method to move the UITextView itself. But it would only work on touches or drags. (Note this was a modified form of touchesBegan & touchesMoved)

Ended up with a UIScrollView with the UITextView as a subview. Now it can move smoothly just that it can be moved from any place on the screen. Not optimal but is the best result to thus keep everything else intact.


Does the textView need to support scrolling? If so, this could get complicated.

But if not, there are two approaches. 1) subclass the textview and override touchesBegan, touchesMoved, touchesEnded. 2) write a gesture recognizer that processes the same messages and attach it to the textview.

Here's an example of a Gesture recognizer that will do the job:

@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

and, how to use it:

- (void)viewDidLoad {
    [super viewDidLoad];

    _textView.scrollEnabled = NO;

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜