iOS条形码动画

WeiXinBarCodeImitate 是仿微信支付条形码的切换动画,包含条形码的生成、截图、旋转动画。
github地址:https://github.com/cheniOS/WeiXinBarCodeImitate

144317_FgAi_2500207.gif

CodeBarViewTool.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CodeBarViewTool : NSObject
+ (UIViewController*)viewControllerWithView:(UIView*)view;
@end

CodeBarViewTool.m

#import "CodeBarViewTool.h"

@implementation CodeBarViewTool
+ (UIViewController*)viewControllerWithView:(UIView*)view {
    for (UIView* next = [view superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}
@end

UIView+CustomAnimation.h

#import <UIKit/UIKit.h>
#import "CodeBarViewTool.h"
#define mainScreenHeight [UIScreen mainScreen].bounds.size.height
#define mainScreenWidth [UIScreen mainScreen].bounds.size.width
@interface UIView (CustomAnimation)
/*
 *根据传入的条形码创建条形码图
 */
+ (void)generateCodeWithImageView:(UIImageView*)imageView code:(NSString*)code;
/*
 *将传入的条形码添加空格
 */
+ (void)insertBlankWithLabel:(UILabel *)label;
/*
 *将传入的view进行截图
 */
+ (UIImage * )getImageWithView:(UIView *)view;
/*
 *根据isShow判断条形码旋转和还原
 */
+ (void)imageViewAnimationWithIsShowView:(UIView*)view  andIsShow:(BOOL)isShow;
 
@end

UIView+CustomAnimation.m

#import "UIView+CustomAnimation.h"

@implementation UIView (CustomAnimation)
//创建条形码
+ (void)generateCodeWithImageView:(UIImageView*)imageView code:(NSString*)code {
    
    // @"CICode128BarcodeGenerator"  条形码
    // @"CIAztecCodeGenerator"       二维码
    NSString *filtername = @"CICode128BarcodeGenerator";
    
    
    CIFilter *filter = [CIFilter filterWithName:filtername];
    [filter setDefaults];
    
    NSData *data = [code dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKey:@"inputMessage"];
    
    CIImage *outputImage = [filter outputImage];
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImage = [context createCGImage:outputImage
                                       fromRect:[outputImage extent]];
    UIImage *image = [UIImage imageWithCGImage:cgImage
                                         scale:1.
                                   orientation:UIImageOrientationUp];
    
    // Resize without interpolating
    CGFloat scaleRate = imageView.frame.size.width / image.size.width;
    UIImage *resized = [UIView resizeImage:image
                             withQuality:kCGInterpolationNone
                                    rate:scaleRate];
    
    imageView.image = resized;
    
    CGImageRelease(cgImage);
}
+ (UIImage *)resizeImage:(UIImage *)image
             withQuality:(CGInterpolationQuality)quality
                    rate:(CGFloat)rate {
    UIImage *resized = nil;
    CGFloat width    = image.size.width * rate;
    CGFloat height   = image.size.height * rate;
    
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, quality);
    [image drawInRect:CGRectMake(0, 0, width, height)];
    resized = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resized;
}

//对条形码添加空格处理
+ (void)insertBlankWithLabel:(UILabel *)label{
    NSMutableString * str =[NSMutableString stringWithString:label.text] ;
    NSInteger num = str.length;
    NSInteger  insertNum = num/4;
    NSInteger  remainder =num%4;
    if (remainder<=3) {
        insertNum = insertNum -1;
    }
    for ( int i = 0; i <insertNum; i++) {
        [ str insertString:@" " atIndex:(i+1)*4+i];
    }
    label.text = str;
    
}
//截图
+ (UIImage * )getImageWithView:(UIView *)view{
    UIGraphicsBeginImageContextWithOptions( view.frame.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return  theImage;
}

//条形码放大旋转/恢复
+ (void)imageViewAnimationWithIsShowView:(UIView*)view  andIsShow:(BOOL)isShow{
    CGPoint oldCenter = view.center;
    CGPoint newCenter = CGPointMake(mainScreenWidth/2, mainScreenHeight/2);
    CABasicAnimation* rotationAnimation1;
    rotationAnimation1 = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
      //旋转
        rotationAnimation1.fromValue = [NSNumber numberWithFloat:isShow?0 : M_PI_2];
        rotationAnimation1.toValue = [NSNumber numberWithFloat:isShow? M_PI_2 :0 ];
        CABasicAnimation* rotationAnimation2= [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    // 缩放/恢复
    
        rotationAnimation2.fromValue = [NSNumber numberWithFloat:isShow? 1.0:1.5];
        rotationAnimation2.toValue = [NSNumber numberWithFloat:isShow? 1.5:1.0];
     
    
    
    CABasicAnimation *rotationAnimation3 =
    [CABasicAnimation animationWithKeyPath:@"position"];
    
    //中心位移
   rotationAnimation3.fromValue = [NSValue valueWithCGPoint:isShow?oldCenter:newCenter ];
   rotationAnimation3.toValue = [NSValue valueWithCGPoint: isShow?newCenter:oldCenter ]; // 終点
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    // 动画选项设定
    group.duration = 0.3;
    group.repeatCount = 1;
    group.removedOnCompletion = NO;
    group.fillMode = kCAFillModeForwards;
    group.animations = [NSArray arrayWithObjects:rotationAnimation1,rotationAnimation2,rotationAnimation3,nil];
    [view.layer addAnimation:group forKey:@"move-rotate-layer"];
    
}
 
@end

CodeBarView.h

#import <UIKit/UIKit.h>
#import "UIView+CustomAnimation.h"
@interface CodeBarView : UIView
/*
 * 初始的条形码图
 */
@property(strong,nonatomic)UIImageView * codeImageView;
/*
 * 点击放大后条形码图
 */
@property(strong,nonatomic)UIImageView * enlargeImageView;
/*
 *初始的条形码label
 */
@property(strong,nonatomic)UILabel * codeLabel;
/*
 *条形码
 */
@property(copy,nonatomic)NSString * codeString;
/*
 *点击后的背景View
 */
@property(strong,nonatomic)UIView * bgView;
/*
 *当前view所在的ViewController
 */
@property(strong,nonatomic)UIViewController * vc;

/*
 *初始view添加的按钮
 */
@property(strong,nonatomic)UIButton * toucheButton;
/*
 *当前view添加的按钮
 */
@property(strong,nonatomic)UIButton * bgButton;
@end

CodeBarView.m

#import "CodeBarView.h"
static CGFloat  codeLabelHeight =  20.0f;

@implementation CodeBarView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor  = [UIColor whiteColor];
        [self addSubview:self.codeImageView];
        [self addSubview:self.codeLabel];
        [self addSubview:self.toucheButton];
        
    }
    return self;
}

-(void)singleTap{
    
    [self addSubviewWhenEnlarge];
   
    [UIView imageViewAnimationWithIsShowView:self.enlargeImageView andIsShow:YES];
   
}
-(void)addSubviewWhenEnlarge{
    self.vc  = [CodeBarViewTool viewControllerWithView:self];
    [self.vc.view addSubview:self.bgView];
    
    CGRect  viewFrame = [self convertRect:self.bounds toView:nil];
    UIImage  * enlargeImage = [UIView getImageWithView:self];
    self.enlargeImageView = [[UIImageView alloc]initWithFrame:viewFrame];
    self.enlargeImageView.image = enlargeImage;
    [self.bgView addSubview:self.enlargeImageView];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.enlargeImageView.userInteractionEnabled = YES;
    [self.bgView addSubview:self.bgButton];

}
-(void)toHidenBgView{
    [UIView imageViewAnimationWithIsShowView:self.enlargeImageView andIsShow:NO];
   
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
//
            for (UIView *v in self.bgView.subviews) {
                [v removeFromSuperview];
            }
            [self.bgView removeFromSuperview];
            [self.vc.navigationController setNavigationBarHidden:NO animated:NO];
        });
    });
    

}
-(void)setCodeString:(NSString *)codeString{
    self.codeLabel.text = codeString;
    [UIView insertBlankWithLabel:self.codeLabel];
    [UIView generateCodeWithImageView:self.codeImageView code:codeString];
}
-(UIButton *)toucheButton{
    if (!_toucheButton) {
        _toucheButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth(self.frame) ,CGRectGetHeight(self.frame))];
    }
    
    [_toucheButton addTarget:self action:@selector(singleTap) forControlEvents:UIControlEventTouchUpInside];
    
    return _toucheButton;
}
-(UIButton *)bgButton{
    if (!_bgButton) {
         _bgButton= [[UIButton alloc]initWithFrame:self.bgView.frame];
    }
 
     [_bgButton addTarget:self action:@selector(toHidenBgView) forControlEvents:UIControlEventTouchUpInside];
    return _bgButton;
}
-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,mainScreenWidth ,mainScreenHeight)];
    }
  
    return _bgView;
}
-(UILabel *)codeLabel{
    if (!_codeLabel) {
         _codeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.codeImageView.frame),CGRectGetWidth(self.frame), codeLabelHeight)];
    }
    _codeLabel.textAlignment = NSTextAlignmentCenter;
    _codeLabel.textColor = [UIColor blackColor];
    return _codeLabel;
}
-(UIImageView *)codeImageView{
    if (!_codeImageView) {
        _codeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)-codeLabelHeight)];
    }
    return _codeImageView;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,128评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,316评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,737评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,283评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,384评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,458评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,467评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,251评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,688评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,980评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,155评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,818评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,492评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,142评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,382评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,020评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,044评论 2 352

推荐阅读更多精彩内容