iOS 阅读器功能小记——自动阅读

一言不合先上效果:

自动阅读GIF.gif

效果分析:

穿透效果:上层view局部变为透明,显示下层view。并非发生大小或位置上的改变。

实现思路:

直接上代码:我是在superController(阅读界面控制器)上添加一个子控制器视图childController,以下代码为childController中代码。

@property (nonatomic, strong) CADisplayLink *link;//帧定时器
@property (nonatomic, strong) CAShapeLayer  *shapeLayer;
@property (nonatomic, strong) UIImageView  *backImage;
@property (nonatomic, strong) UIImageView  *shadowImage;

@property (nonatomic, assign) CGFloat topY;
@property (nonatomic, assign) CGFloat speed;

@property (nonatomic, assign) BOOL isPaused;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _backImage = [[UIImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:_backImage];
    //创建mask需要的形状
    _shapeLayer = [CAShapeLayer layer];
    _backImage.layer.mask = _shapeLayer;
    //阴影效果需要的图片
    _shadowImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 5)];
    _shadowImage.image = [UIImage imageNamed:[self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight] ? @"shadow_line_night":@"shadow_line_dawn"];
    [self.view addSubview:_shadowImage];
    //添加点击手势,暂停动画
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [self.view addGestureRecognizer:tap];
}

- (void)updateWithView:(UIImage *)image {
    _shapeLayer.path = [UIBezierPath bezierPathWithRect:self.view.frame].CGPath;
    self.backImage.image = image;
}

这里说明一下,backImage显示的是superController的view,通过截屏的方式生成image。下面是截屏的代码。

- (UIImage *)captureView:(UIView *)view {
    CGRect rect = view.bounds;
    UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
开始做动画:

需要注意的是mask只关注具体的形状路径,和本身的颜色无关,所以扫描的阴影样式我使用图片来实现。

- (void)startAuto {
    if (_link == nil) {
        // 启动定时调用
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrent:)];
        [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
}

-(void)getCurrent:(CADisplayLink *)displayLink{
    self.isRunning = YES;
    if(_topY >= self.view.frame.size.height) {
        _topY = 0;
        self.shadowImage.center = CGPointMake(self.view.center.x, self.shadowImage.bounds.size.height/2);
        
        [_link invalidate];
        _link = nil;
        
        [_delegate finishReadPage:self];
        return;
    }
    _topY += _speed;
    [self setCurrentShadowLayer];
    [self setCurrentMaskLayer];
}

- (void)setCurrentMaskLayer {
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, 0, _topY);
    CGPathAddLineToPoint(path, nil, self.view.frame.size.width, _topY);
    CGPathAddLineToPoint(path, nil, self.view.frame.size.width, self.view.frame.size.height);
    CGPathAddLineToPoint(path, nil, 0, self.view.frame.size.height);
    CGPathCloseSubpath(path);
    
    _shapeLayer.path = path;
    CGPathRelease(path);
}

- (void)setCurrentShadowLayer {
    [UIView animateWithDuration:0.1 animations:^{
        CGPoint pos = self.shadowImage.center;
        pos.y += _speed;
        self.shadowImage.center = pos;
    }];
}
控制暂停
- (void)singleTap {
    _isPaused ^= 1;
    [_link setPaused:_isPaused];
}

小结:

我是通过水波动画的实现想到这样实现的,通过CADisplayLink修改shapeLayer的path从而实现view的局部透明效果。可能我的方案不是最佳方案,如果大家有更好的方案可以留言给我,一起学习,谢谢。

补充:

看到评论区很多朋友问我有没有demo,所以我做了一个简单的demo放在GitHub上。希望用到觉得可以的朋友给我点个star或喜欢。谢谢大家。
下载地址:https://github.com/rabbitmouse/ZQAutoReadBookDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,222评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,613评论 25 709
  • 很遗憾觉得学习了这么多年写作一直是我的弱项,我会悄悄的回避,但是总觉得回避是不对的,希望能慢慢培养写作的能力,不再害怕。
    亨利来了阅读 2,720评论 1 1
  • 数据库键空间 Redis 是一个键值对(key-value pair)数据库服务器, 服务器中的每个数据库都由一个...
    颜灏_2181阅读 3,940评论 0 1
  • 我有一个梦想。 大家都知道的。 你有什么梦想? 是….. 是…… 是…… 是…… 是……
    心之物语阅读 1,306评论 0 0