扩大UIPageViewController的点击范围
阅读原文时间:2023年07月08日阅读:2

UIPageViewController中的边缘点击手势大概是屏幕的1/6,市面的大多数阅读器点击手势都在1/3以上,或者我干脆想自定义点击的范围,但又不想放弃系统的翻页效果,这时候该怎么做了?其实很简单,听我细细说来。

1.首先禁用UIPageViewController的tap手势(如果不禁用,会造成多次翻页)

可以创建UIPageViewController的分类,重写shouldReceiveTouch:方法即可

@implementation UIPageViewController (EnlargeTapRegion)

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
return NO;
}
return YES;
}

@end

2.自定义tap手势,实现touchReceiveTouch中的有效点击及响应的点击方法

- (void)tapSelf:(UIGestureRecognizer *)gesture {
NSLog(@"receive tap action !!!");
UIView *touchView = gesture.view;
CGPoint touchPoint = [gesture locationInView:touchView];
CGFloat viewWidth = CGRectGetWidth(touchView.frame);
if (touchPoint.x < viewWidth/3) { _page -= 2; UIViewController *preVC = [self previousVC]; BackPageViewController *backVC = [self backVCAt:[self previousVC]]; [self.pageVC setViewControllers:@[preVC,backVC] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil]; }else if (touchPoint.x > viewWidth/3*2) {
_page += 2;
UIViewController *nextVC = [self nextVC];
BackPageViewController *backVC = [self backVCAt:[self nextVC]];
[self.pageVC setViewControllers:@[nextVC,backVC] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
}

  • (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    UIView *touchView = gestureRecognizer.view;
    CGPoint touchPoint = [gestureRecognizer locationInView:touchView];
    CGFloat viewWidth = CGRectGetWidth(touchView.frame);
    if (gestureRecognizer == _weakTap) {
    if (touchPoint.x < viewWidth/3 || touchPoint.x > viewWidth/3*2) {
    return YES;
    }
    }
    return NO;
    }

      -shouldReceiveTouch方法中判断当前的触点X是否规定区域内,否则返回NO

       -tapSelf方法中根据触点位置判断翻页方向,使用pageViewController的方法setViewController:direction:方法实现手动跳转

        注意:使用这种方式跳转,不会常规走pageViewController的代理方法,需要自行处理数据的同步

pageviewController的双面模式实现,仅仅在pageCurl翻页模式下存在

1.设置doubleSide属性为YES

2.viewControllerBeforeViewController:及viewControllerAfterViewController代理方法中根据页面返回相应的控制器

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
_page -= 1;
if (_page % 2 == 1) {
return [self backVCAt:[self previousVC]];
}else {
return [self previousVC];
}
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
_page += 1;
if (_page % 2 == 1) {
return [self backVCAt:[self nextVC]];
}else {
return [self nextVC];
}
}

3.当前视图的左右翻转透明显示

自定义控制器里面添加imageView,外界传入原本控制器或控制器视图,对该视图进行翻转绘制成图片赋给imageView

-(UIImage *)captureWithView:(UIView *)view
{
CGRect rect = view.bounds;
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, rect.size.width, 0.0);
CGContextConcatCTM(context, transform);
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

github 传送门

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章