iOS条形码动画

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

iOS条形码动画_第1张图片
144317_FgAi_2500207.gif

CodeBarViewTool.h

#import 
#import 
@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 
#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 

CodeBarView.h

#import 
#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

你可能感兴趣的:(iOS条形码动画)