使用 UIPanGestureRecognizer 时如何将超级视图与视图一起居中?
Posted
技术标签:
【中文标题】使用 UIPanGestureRecognizer 时如何将超级视图与视图一起居中?【英文标题】:How to center superview along with view when using 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];
【问题讨论】:
【参考方案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];
【讨论】:
以上是关于使用 UIPanGestureRecognizer 时如何将超级视图与视图一起居中?的主要内容,如果未能解决你的问题,请参考以下文章
为啥使用 UIPanGestureRecognizer 移动对象时会有延迟?
UIPanGestureRecognizer ***视图未获取事件,子视图使用它们
在 UITableView 上滑动删除以使用 UIPanGestureRecognizer
在 UIPanGestureRecognizer 中使用velocityInView