zoukankan      html  css  js  c++  java
  • iOS开发——刮奖

      还是直接上代码,有什么问题的话,直接评论。

      1.在YYTScratchView.h文件中

    //

    //  YYTScratchView.h

    //  Demo-刮奖

    //

    //  Created by yyt on 16/4/20.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import <UIKit/UIKit.h>

    @interface YYTScratchView : UIView

    @property (nonatomic,assign) float sizeBrush;

    -(void)setHideView:(UIView*) hideView;

    @end

      2在YYTScratchView.m文件中

    //

    //  YYTScratchView.m

    //  Demo-刮奖

    //

    //  Created by yyt on 16/4/20.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import "YYTScratchView.h"

    @interface YYTScratchView (){

        CGContextRef _contextMask;//maskContext 用户touch 改变的context

        CGImageRef _scratchCGImg;//CGimageRef 封装_contextMask 图片信息 _contextMask改变 跟着改变 直到 调用生成UIImage

        CGPoint currPonit;

        CGPoint prePoint;

    }

    @end

    @implementation YYTScratchView

    - (id)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            [self setOpaque:NO];

            //设置透明 如果不透明就没法看到下一层了

            self.sizeBrush=10.0f;

        }

        return self;

    }

    - (void)drawRect:(CGRect)rect

    {

        [super drawRect:rect];

        UIImage* imageToDraw=[UIImage imageWithCGImage:_scratchCGImg];

        [imageToDraw drawInRect:self.frame];

    }

    //setSizeBrush before setHideView

    -(void)setHideView:(UIView*) hideView{

        CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceGray();

        CGFloat scale = [UIScreen mainScreen].scale;

        

        //获得当前传入View的CGImage

        UIGraphicsBeginImageContextWithOptions(hideView.bounds.size, NO, 0);

        hideView.layer.contentsScale=scale;

        [hideView.layer renderInContext:UIGraphicsGetCurrentContext()];

        CGImageRef hideCGImg=UIGraphicsGetImageFromCurrentImageContext().CGImage;

        UIGraphicsEndImageContext();

        

        //绘制Bitmap掩码

        size_t width=CGImageGetWidth(hideCGImg);

        size_t height=CGImageGetHeight(hideCGImg);

        

        CFMutableDataRef pixels;

        pixels=CFDataCreateMutable(NULL, width*height);

        //创建一个可变的dataRef 用于bitmap存储记录

        _contextMask = CGBitmapContextCreate(CFDataGetMutableBytePtr(pixels), width, height , 8, width, colorSpace, kCGImageAlphaNone);

        

        //数据提供者

        CGDataProviderRef dataProvider=CGDataProviderCreateWithCFData(pixels);

        

        //填充黑色背景 mask中黑色范围为显示内容 白色为不显示

        CGContextSetFillColorWithColor(_contextMask, [UIColor blackColor].CGColor);

        CGContextFillRect(_contextMask, self.frame);

        

        CGContextSetStrokeColorWithColor(_contextMask, [UIColor whiteColor].CGColor);

        CGContextSetLineWidth(_contextMask, self.sizeBrush);

        CGContextSetLineCap(_contextMask, kCGLineCapRound);

        

        CGImageRef mask=CGImageMaskCreate(width, height, 8, 8, width, dataProvider, nil, NO);

        _scratchCGImg=CGImageCreateWithMask(hideCGImg, mask);

        

        CGImageRelease(mask);

        CGColorSpaceRelease(colorSpace);

        

    }

    -(void)scratchViewFrom:(CGPoint)startPoint toEnd:(CGPoint)endPoint{

        float scale=[UIScreen mainScreen].scale;

        //CG的Y与UI的是反的 UI的y0在左上角 CG在左下

        CGContextMoveToPoint(_contextMask, startPoint.x*scale, (self.frame.size.height-startPoint.y)*scale);

        CGContextAddLineToPoint(_contextMask, endPoint.x*scale,(self.frame.size.height-endPoint.y)*scale);

        CGContextStrokePath(_contextMask);

        [self setNeedsDisplay];

    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        [super touchesBegan:touches withEvent:event];

    }

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

        [super touchesMoved:touches withEvent:event];

        UITouch *touch=[touches anyObject];

        currPonit=[touch locationInView:self];

        prePoint=[touch previousLocationInView:self];

        [self scratchViewFrom:prePoint toEnd:currPonit];

    }

    -(void)toucheseEnd:(NSSet *)touches withEvent:(UIEvent *)event{

        [super touchesEnded:touches withEvent:event];

        UITouch *touch=[touches anyObject];

        currPonit=[touch locationInView:self];

        prePoint=[touch previousLocationInView:self];

        [self scratchViewFrom:prePoint toEnd:currPonit];

    }

    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

        [super touchesCancelled:touches withEvent:event];

    }

    @end

      3.在需要调用的地方

    //

    //  ViewController.m

    //  Demo-刮奖

    //

    //  Created by yyt on 16/4/20.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import "ViewController.h"

    #import "YYTScratchView.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

        imageView.image = [UIImage imageNamed:@"11.png"];

        [self.view addSubview:imageView];

        

        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

        imageView2.image = [UIImage imageNamed:@"22.png"];

        //[self.view addSubview:imageView];

        

        YYTScratchView *scratchView = [[YYTScratchView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];

        scratchView.sizeBrush = 20.0;

        [scratchView setHideView:imageView2];

        [self.view addSubview:scratchView];

    }

    @end

  • 相关阅读:
    删除所有空白列 cat yum.log | awk '{$1=$2=$3=$4=null;print $0}'>>yum.log1 sed ‘s/[ ]*$//g' 删除所有空格 sed -i s/[[:space:]]//g yum.log
    make clean 清除之前编译的可执行文件及配置文件。 make distclean 清除所有生成的文件。
    ipmitool -I lanplus -H 10.1.81.90 -U admin -P admin mc reset cold
    netperf对比
    iozone
    CentOS 7 vs. CentOS 8 版本差异大比拼
    seajs模块化jQuery与jQuery插件【转】
    教你怎么写jQuery的插件
    Jquery特效之=》仿京东多条件筛选特效
    sql FOR XML PATH('')
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5415699.html
Copyright © 2011-2022 走看看