如何使用UIPanGestureRecognizer在屏幕中移动多个控件

时间:2016-02-19 05:20:04

标签: ios ipad uipangesturerecognizer

如何使用UIPanGestureRecognizer移动屏幕中的多个控件以及如何识别当前移动的控件。

示例:

我在视图中使用UITextFieldUIButtonUIImageView。我可以通过为每个控件使用不同的实例来移动多个控件,但是平移的方法是相同的方法。

请帮我找出目前我在运行时平移的控件。

1 个答案:

答案 0 :(得分:0)

1)以下是设置pag手势的方法

- (void)setupGestures
{
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(movePanel:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [self.view addGestureRecognizer:panRecognizer];
}

2)只要视图移动

,就会调用此方法
  -(void)movePanel:(id)sender
    {

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

    //here are different states from which you can change view

        if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
        {


        }

        if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateChanged) {

    // Are you more than halfway? If so, show the panel when done dragging by setting this value to YES (1).
    // Here you can change the position of the component depending on translation point 
   // translatedPoint.x & translatedPoint.y  keep on changing as you move view over screen


        }
    }

3)为了检查哪个视图正在移动,您可以通过标签&从该标签识别视图&改变框架