Ios UIPageViewController检测平移手势
在左/右滑动时,是否有方法确定UIPageViewController的平移位置?我一直在努力做到这一点,但它不起作用。我有一个UIPageViewController作为子视图添加,我可以水平向左/向右滑动它以在页面之间切换,但是我需要确定我在屏幕上平移的位置的x,y坐标 我知道怎么做了。基本上,UIPageViewController使用UIScrollView作为其子视图。我创建了一个循环,设置了所有属于UIScrollView的子视图,并将它们的代理指定给了我的ViewControllerIos UIPageViewController检测平移手势,ios,uipageviewcontroller,uipangesturerecognizer,Ios,Uipageviewcontroller,Uipangesturerecognizer,在左/右滑动时,是否有方法确定UIPageViewController的平移位置?我一直在努力做到这一点,但它不起作用。我有一个UIPageViewController作为子视图添加,我可以水平向左/向右滑动它以在页面之间切换,但是我需要确定我在屏幕上平移的位置的x,y坐标 我知道怎么做了。基本上,UIPageViewController使用UIScrollView作为其子视图。我创建了一个循环,设置了所有属于UIScrollView的子视图,并将它们的代理指定给了我的ViewControlle
/**
* Set the UIScrollViews that are part of the UIPageViewController to delegate to this class,
* that way we can know when the user is panning left/right
*/
-(void)initializeScrollViewDelegates
{
UIScrollView *pageScrollView;
for (UIView* view in self.pageViewController.view.subviews){
if([view isKindOfClass:[UIScrollView class]])
{
pageScrollView = (UIScrollView *)view;
pageScrollView.delegate = self;
}
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"Im scrolling, yay!");
}
我个人的偏好是不要太依赖PageViewController的内部结构,因为它可以在以后更改,这将破坏您的代码,您不知道 我的解决方案是使用平移手势识别器。在
viewDidLoad
中,添加以下内容:
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handler))
gestureRecognizer.delegate = yourDelegate
view.addGestureRecognizer(gestureRecognizer)
在您的yourDelegate
的定义中,您应该实现以下方法,以允许您的手势识别器处理触摸
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
现在,您应该能够访问用户触摸的X/Y位置:
func handler(_ sender: UIPanGestureRecognizer) {
let totalTranslation = sender.translation(in: view)
//...
}