支付宝UI篇-扫码和AR

简介: 这里的UI实现,主要考验遮罩蒙板的使用。 UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds]; UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcC

这里的UI实现,主要考验遮罩蒙板的使用。


具体看代码实现:代码链接


#import "IRCameraMask.h"

@implementation IRCameraMask
{
    CALayer *_bgLayer;
    CALayer *_mainLayer;
    CALayer *_scanLayer;
    
    float _radius;
    
    CABasicAnimation *_moveAnimation;
    CALayer *_moveLayer;
}

- (instancetype)init
{
    return [self initWithFrame:[UIScreen mainScreen].bounds];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame type:2];
}

- (instancetype)initWithFrame:(CGRect)frame type:(int)type;
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _type = type;
        
        float edge    = 37;
        float radius2 = (self.bounds.size.width - 2 *edge);
        _radius = radius2/2.0;
        
        _bgLayer = [CALayer layer];
        [self.layer addSublayer:_bgLayer];
        
        float width = radius2+2;
        _scanLayer = [CALayer layer];
        _scanLayer.frame = CGRectMake(0, 0, width , width);
        [self.layer addSublayer:_scanLayer];
        
        CALayer *maskLayer = [CALayer layer];
        maskLayer.frame    = _scanLayer.bounds;
        _scanLayer.mask    = maskLayer;
        
        CALayer *moveLayer = [CALayer layer];
        moveLayer.frame    = _scanLayer.bounds;
        [_scanLayer addSublayer:moveLayer];
        
        CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
        moveAnimation.duration    = 1.75;
        moveAnimation.repeatCount = HUGE_VALF;
        [moveLayer addAnimation:moveAnimation forKey:@"AnimationMoveY"];
        
        _moveAnimation = moveAnimation;
        _moveLayer     = moveLayer;
        
        _mainLayer = [CALayer layer];
        [self.layer addSublayer:_mainLayer];
        
        _bgLayer.frame    = self.bounds;
        _mainLayer.frame  = self.bounds;
        
        [self changeType:_type];
    }
    return self;
}

- (void)changeType:(int)type
{
    _type = type;
    
    float offY      = 25;
    float fromValue = -_radius;
    float toValue   = _radius + offY;
    NSString *maskName = @"camera-loupe-mask";
    NSString *moveNmae = @"5";
    UIImage *mainImage = [UIImage imageNamed:@"camera-mask"];
    
    CGPoint scanCenter = CGPointMake(self.center.x,self.center.y - offY);
    
    CGRect rect = CGRectMake(scanCenter.x - _radius, scanCenter.y - _radius, _radius*2, _radius*2);
    
    _scanRect      = rect;
    _scanScaleRect = [self getScanCrop:rect readerViewBounds:self.bounds];
    
    if (_type == 1)
    {
        fromValue   = scanCenter.y - 3*_radius;
        toValue     = scanCenter.y - _radius;
        maskName    = @"scan-loupe-mask";
        moveNmae    = @"scan_net";
        mainImage   = [self getRectImageWithRect:rect];
        
        _mainLayer.mask = nil;
    } else
    {
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
        UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:scanCenter
                                                                  radius:_radius
                                                              startAngle:0
                                                                endAngle:2 *M_PI
                                                               clockwise:NO];
        [path appendPath:circlePath];
        
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = path.CGPath;
        _mainLayer.mask = shapeLayer;
    }
    _moveLayer.contents      = (id)[UIImage imageNamed:moveNmae].CGImage;
    _scanLayer.mask.contents = (id)[UIImage imageNamed:maskName].CGImage;
    _scanLayer.center        = scanCenter;
    _moveAnimation.fromValue = [NSNumber numberWithFloat:fromValue];
    _moveAnimation.toValue   = [NSNumber numberWithFloat:toValue];
    _mainLayer.contents      = (id)mainImage.CGImage;
    
    [self stopAnimating];
    [self startAnimating];
}

- (void)startAnimating
{
    [_moveLayer addAnimation:_moveAnimation forKey:@"AnimationMoveY"];
}

- (void)stopAnimating
{
    [_moveLayer removeAnimationForKey:@"AnimationMoveY"];
}

- (void)setImage:(UIImage *)image
{
    _image = image;
    _bgLayer.contents = (id)image.CGImage;
}

- (UIImage *)getRectImageWithRect:(CGRect)rect
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 1.0);
    CGContextRef con = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(con, [UIColor colorWithWhite:0 alpha:0.45].CGColor);
    CGContextFillRect(con, self.bounds);
    
    CGRect rects[] =
    {
        rect,
    };
    CGContextAddRects(con, rects, sizeof(rects)/sizeof(CGRect));
    
    CGContextSetBlendMode(con, kCGBlendModeClear);
    CGContextFillPath(con);
    
    UIImage *ima = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return ima;
}


#pragma mark-> 获取扫描区域的比例关系

- (CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{
    CGFloat x,y,width,height;
    
    x = (CGRectGetHeight(readerViewBounds)-CGRectGetHeight(rect))/2/CGRectGetHeight(readerViewBounds);
    y = (CGRectGetWidth(readerViewBounds)-CGRectGetWidth(rect))/2/CGRectGetWidth(readerViewBounds);
    width  = CGRectGetHeight(rect)/CGRectGetHeight(readerViewBounds);
    height = CGRectGetWidth(rect)/CGRectGetWidth(readerViewBounds);
    
    return CGRectMake(x, y, width, height);
}

@end



目录
相关文章
|
算法 JavaScript 前端开发
支付宝五福 AR 玩法及背后的互动引擎—Paladin
支付宝五福 AR 玩法及背后的互动引擎—Paladin
686 0
|
编解码 前端开发 JavaScript
支付宝 AR 空中写福技术揭秘
支付宝 AR 空中写福技术揭秘
7376 0
|
算法 关系型数据库 数据库
支付宝(AR虚拟现实)红包 玩法与技术背景 - GIS(LBS)、图像识别与秒杀技术的完美结合
背景 作为搞IT的小伙伴们,对虚拟现实(AR)应该并不陌生,这次支付宝带来了一个很有趣的功能,虚拟现实与藏红包结合起来。 简单介绍一下这个业务,然后我们再来思考背后的技术。 支付宝(AR虚拟现实)藏红包、找红包 玩法介绍 想象一下,你可以把你的红包藏在世界各地的任意角落,让全世界的人来找红包
10261 0
|
传感器 算法 vr&ar
从“扫月亮”到“扫福字”,扒一扒背后的支付宝AR框架体系
从攒五福到抢红包,全国人民的春节活动越来越多样,其背后技术挑战也更复杂,每一年的红包背后,如果能拍摄出来,都将是一部技术大片,在云栖社区2017红包技术峰会上,蚂蚁金服技术专家承智为大家分享了“扫福字”的背后的支付宝AR框架体系实践,从支付宝AR四个实际案例出发,总结了支付宝2016年在AR技术方面所获取的经验并分享了对于AR技术未来的发展方向的展望。
7703 0
|
机器学习/深度学习 算法 固态存储
|
算法 vr&ar
说一说关于破解支付宝AR红包的事
当朋友圈的你们才开始分享支付宝AR红包的消息的时候,我已经对它动了一二三四次歪脑筋了,虽然事实证明并不是那么顺利,至今我也只在电脑前识别出5个不知道在哪里的红包,其中一个还因为定位信息不符开不了。
937 0