简体   繁体   English

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

[英]How to use UIPanGestureRecognizer for moving multiple controls in the screen

How do I use UIPanGestureRecognizer for moving multiple controls in the screen and how to identify which control in currently moving. 如何使用UIPanGestureRecognizer在屏幕上移动多个控件,以及如何识别当前正在移动的控件。

Example: 例:

I'm using UITextField , UIButton and UIImageView in a view. 我在视图中使用UITextFieldUIButtonUIImageView I'm able to move multiple controls by using the different instantiate for each control but the method for panning is same method. 我可以通过对每个控件使用不同的实例化来移动多个控件,但是平移方法是相同的方法。

Please help me find out which control currently I'm panning in runtime. 请帮助我找出当前正在运行时平移哪个控件。

1)Here is how you can set the pag gesture 1)这里是设置手势的方法

- (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)This method gets called whenever the view moves 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)For checking which view is moving you can check the view by tag & identify the view from that tag & change the frame 3)要检查哪个视图正在移动,可以按标签检查视图,并从该标签识别视图并更改框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM