iOS開發——刮獎

來源:互聯網
上載者:User

標籤:

  還是直接上代碼,有什麼問題的話,直接評論。

  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

 

iOS開發——刮獎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.