使用UIPanGestureRecognizer时如何将superview与视图对齐?

时间:2013-12-09 08:47:37

标签: ios draggable uipangesturerecognizer

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

tinypic.com/r/2qs191g/5

请帮我修理一下?(如果我拖动视图,那么两者都应该移动到同一位置)

我的代码如下

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

 }

1 个答案:

答案 0 :(得分: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];

}