1
我創建了一個示例應用程序,當我們點擊一個按鈕時出現另一個視圖(ResizingViewController)視圖。 當我拖動ResizingViewController時,它正在正確移動,但我可以看到它背後的另一個視圖。我認爲它是超級視圖(如圖中所示,但不能同時移動)。如何在使用UIPanGestureRecognizer時將超級視圖與視圖集中在一起?
請幫我解決這個問題?(如果我拖到視圖,則兩者都應該移動到相同的位置)
我的代碼如下
- (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];
}