簡體   English   中英

我如何在視圖屏幕周圍拖動UITextView?

[英]How would I drag UITextView around the view screen?

我有一個項目,可以在屏幕上拖動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;
 }
}
- (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后調用此方法。

好吧,還不能操縱實際的uitextview。

首先嘗試制作一個可以移動並可以按下以開始編輯的按鈕疊加層,但是它沒有正確居中。

然后嘗試上述方法移動UITextView本身。 但這僅適用於觸摸或拖動。 (請注意,這是touchesBegan和touchesMoved的修改形式)

最后得到一個UIScrollView,並將UITextView作為子視圖。 現在,它可以平滑移動,就像它可以從屏幕上的任何位置移動一樣。 這不是最佳的,但是是保持其他所有內容不變的最佳結果。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM