我一直在尝试修复UIPageViewControllers的手势与视图中添加的手势以及位于页面控制器内的UIViewControllers之间的冲突。 我只需要让UIPageViewController成为其手势的委托我该怎么做?
如何设置UIPageViewController手势(特别是Pan Gesture)代表。
答案 0 :(得分:1)
这不会为UIPageViewController的内置Pan手势设置手势委托,但我有些如何设法通过获取底层的scrollview然后在滚动视图上设置新的Pan手势来解决手势冲突。这个新的Pan手势将UIPageViewController作为其委托,因此我可以调用手势委托。
-(UIScrollView *)scrollViewInsidePageControl {
for (UIView * view in self.view.subviews) {
if([view isKindOfClass:[UIScrollView class]])
return (UIScrollView *)view;
}
return nil;
}
并在-viewDidLoad:
self.internalScrollView = [self scrollViewInsidePageControl];
self.customPanGesture = [UIPanGestureRecognizer new];
self.customPanGesture.delegate = self;
[self.internalScrollView addGestureRecognizer:_customPanGesture];
以上代码调用了UIGestureRecognizerDelegate
实例上UIPageViewController
的协议方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if(otherGestureRecognizer.view == self.internalScrollView)
return NO;
else
return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == _customPanGesture)
{
// Did required handling here
}
return YES;
}
答案 1 :(得分:0)
要设置UIPageViewController委托,只需将“UIPageViewControllerDelegate”和“UIPageViewControllerDataSource”添加到您的UIPageViewController即可。然后将UIPageViewController的委托和dataSource设置为self(委托)。
class MyPageViewController : UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
}
}
如果您没有设置dataSource,您将无法在视图之间使用UIPageViewController幻灯片手势。