【问题标题】:How to center superview along with view when using UIPanGestureRecognizer?使用 UIPanGestureRecognizer 时如何将超级视图与视图一起居中?
【发布时间】:2013-12-26 07:41:20
【问题描述】:

我创建了一个示例应用程序,当我们单击一个按钮时,会出现另一个视图(ResizingViewController)视图。 当我拖动 ResizingViewController 时,它正在正确移动,但我可以看到它后面的另一个视图。我认为它是超级视图(如图所示,但不能同时移动)。

请帮我修复它?(如果我拖动视图,那么两者都应该移动到相同的位置)

我的代码如下

- (IBAction)click_Action:(id)sender {

      if (!resizingViewController) {
          return;
      }

      //Creating Right Button, asigning a method to it.
      UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Close"   
            style:UIBarButtonItemStylePlain target:self action:@selector(done)];
            [resizingViewController.navigationItem setRightBarButtonItem:rightButton];

      theNavigationController = [[UINavigationController alloc]    
            initWithRootViewController:resizingViewController];
      [theNavigationController.navigationBar setTintColor:[UIColor blueColor]];
      if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            theNavigationController.modalPresentationStyle  
                                      =UIModalPresentationFormSheet;
      }
      [self presentViewController:theNavigationController animated:YES completion:nil];

      CGRect r = CGRectMake(100, 100, 300, 400);
      r = [self.view convertRect:r toView:self.view];
      theNavigationController.view.superview.frame = r;

      //Adding gesture reconizer to resizingviewcontroller
      UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] 
                initWithTarget:self action:@selector(handlePanFrom:)];
      [theNavigationController.view addGestureRecognizer:panGestureRecognizer];
}

-(void)handlePanFrom:(UIPanGestureRecognizer *) recognizer
{    
       CGPoint translation = [recognizer translationInView:recognizer.view];

       //Changing Center of the the view as soon as user selects the view and drags
       recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                            recognizer.view.center.y +  translation.y);
       CGPoint r = CGPointMake(recognizer.view.center.x+100, 
                                            recognizer.view.center.y+100);
       r = [self.view convertPoint:r toView:self.view];
       recognizer.view.superview.center = r;
       //Should not forget to give below code, else view goes out of bounds.
       [recognizer setTranslation:CGPointZero inView:self.view];

 }

【问题讨论】:

    标签: ios draggable uipangesturerecognizer


    【解决方案1】:

    好的,我解决了这个问题。(将其标记为答案,以便如果任何人有同样的问题可以使用它)

    -(void)handlePanFrom:(UIPanGestureRecognizer *) recognizer
    

    {

          CGPoint translation = [recognizer translationInView:recognizer.view.superview];
          recognizer.view.superview.center =   
                      CGPointMake(recognizer.view.superview.center.x+translation.x, 
                      recognizer.view.superview.center.y+translation.y);
          [recognizer setTranslation:CGPointZero inView:self.view];
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多