最近在做阅读器,使用原生控件UIPageViewController是发现翻页逻辑是不能通过dataSource中的before和after代理来控制的,因为如果从里头拿index,这个index在PageVC内部并不是和翻页逻辑一致的.
-
所以我采取在willTransitionTo代理中来处理翻页逻辑
需要注意的是,并不是每一个翻页动作都会出发翻页逻辑,只要dataSource提供正确数据就行,没必要保证每个翻页动作都触发翻页逻辑. -
但是在触发翻页动作时必须保证翻页逻辑的正确. 所以每一个翻页动作细分为6中情况, 分别是
同章下翻
末页下翻
首页下翻
同章上翻
末页上翻
首页上翻
这6种情况的判断逻辑各不相同,可能有人会有更加优秀的写法,反正我就用土办法if else if
写了. -
大致代码如下,代码中的pageChap对象和cp_will和cp_down对象都是同一个类,表示当前页的内容模型与页码信息.
//翻页即将开始
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers{
@weakify(self)
self.dataControl.isPaging = YES;
[pendingViewControllers enumerateObjectsUsingBlock:^(UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
@strongify(self)
RenderVC *render = (RenderVC *)obj;
BookChapPage *cp = render.chapPage;
self.cp_Will = cp;
NSLog(@"翻页开始%ld",cp.chapIndex);
NSLog(@"willCount:%ld, downCount:%ld",self.cp_Will.pageCount,self.cp_down.pageCount);
NSLog(@"cpIndex:%ld,cpDown:%ld",cp.chapIndex,self.cp_down.chapIndex);
//同章下翻
if (self.cp_Will.pageCount > self.cp_down.pageCount && cp.chapIndex == self.cp_down.chapIndex) {
self.page_dt = Page_Direct_Forward;
}
//末页下翻
else if (self.cp_Will.pageCount == 1 && cp.chapIndex > self.cp_down.chapIndex) {
self.page_dt = Page_Direct_Forward;
}
//首页下翻
else if (self.cp_down.pageCount == self.cp_down.totalCount && cp.chapIndex > self.cp_down.chapIndex) {
self.page_dt = Page_Direct_Forward;
}
//末页上翻
else if (self.cp_Will.pageCount == self.cp_Will.totalCount-1 && cp.chapIndex < self.cp_down.chapIndex) {
self.page_dt = Page_Direct_Back;
}
//同章上翻
else if (self.cp_Will.pageCount < self.cp_down.pageCount && cp.chapIndex == self.cp_down.chapIndex) {
self.page_dt = Page_Direct_Back;
}
//首页上翻
else if (self.cp_Will.pageCount == self.cp_Will.totalCount && cp.chapIndex < self.cp_down.chapIndex) {
self.page_dt = Page_Direct_Back;
}
else{
}
[self pageChap:cp direction:self.page_dt];
}];
}
网友评论