是否有一种方法可以确定UIPageViewController在左/右滑动时的摇摄位置?我一直在努力实现这一目标,但这并不奏效。我增加了一个UIPageViewController作为一个子视图,我可以将它水平地左/右滑动,以在页面之间切换,但是我需要确定我在屏幕上的摇摄位置的x,y坐标。
发布于 2015-03-11 18:34:49
我想出了怎么做。基本上,UIPageViewController使用UIScrollViews作为它的子视图。我创建了一个循环,设置了所有属于UIScrollViews的子视图,并将它们的委托分配给了我的ViewController。
/**
* 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!");
}发布于 2016-12-01 03:39:43
我个人的偏好是不太依赖于PageViewController的内部结构,因为它以后可能会被更改,这会破坏您的代码,这是您不知道的。
我的解决方案是使用pan手势识别器。在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)
//...
}https://stackoverflow.com/questions/28949537
复制相似问题