国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[ios]ios-Demo4脫衣服/刮獎app-專業

2019-11-14 19:41:11
字體:
來源:轉載
供稿:網友

普通版本完成的鋸齒很嚴重 但是Ios系統中僅CGContextClearRect 并不存在cyclo等方法。 網上查了一些資料。 發現還是利用到了CG 中的Mask來實現
效果圖:
  


這種效果可以自定義畫筆的粗細等相關設置,不會像普通模式中出現比較嚴重的鋸齒。

具體分析如下:
     1.獲取需要隱藏View的layer的CGImageRef用于后面配合掩碼
     2.開辟符合1的CGImageRef的CFMutableDataRef空間 給下面的掩碼BitmapContext使用
     3.通過CFMutableDataRef獲得CGDataPRoviderRef (maskCreate用到)
     3.BitmapContextCreate
     4.BitmapContext填充黑色 并且設置畫筆相關(白色,寬度,線帽(cap))
     5.CGImageMaskCreate 將CGDataProviderRef  傳入 則得到CGImageRef指向的是剛才創建BitmapContext。改變BitmapContext 則此CGImageRef內容也會改變
     6.CGImageCreateWithMask - 通過Mask的CGImageRef創建CGImageRef 此CGImageRef隨 Maskde CGImageRef變化(所以 BitmapContext改變 Mask改變 mask掩碼后的CGImageRef改變)
     7.釋放

     8.手指觸摸時,根據上一個CGPoint 和此次的CGPoint 給bitmapContext新增path 并且繪制。然后調用【self setNeedsDisplay】,在重繪中(-drawRect) 重新獲取通過mask后的CGImageRef 獲取最新的UIImage
     9.將最新的UIImage drawRect到界面


代碼:
//
//  PLLScrathView.h
//  PLLScratchViewDemo
//
//  Created by liu poolo on 14-7-31.
//  Copyright (c) 2014年 liu poolo. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface PLLScratchView : UIView
@property (nonatomic,assign) float sizeBrush;
-(void)setHideView:(UIView*) hideView;
@end

//
//  PLLScrathView.m
//  PLLScratchViewDemo
//
//  Created by liu poolo on 14-7-31.
//  Copyright (c) 2014年 liu poolo. All rights reserved.
//

#import "PLLScratchView.h"
@interface PLLScratchView(){
    CGContextRef _contextMask;//maskContext 用戶touch 改變的context
    CGImageRef _scratchCGImg;//CGimageRef 封裝_contextMask 圖片信息 _contextMask改變 跟著改變 直到 調用生成UIImage
    CGPoint currPonit;
    CGPoint prePoint;
}
@end

@implementation PLLScratchView
@synthesize sizeBrush;
- (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

 

  • 大小: 566.6 KB

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 萨迦县| 渑池县| 萨嘎县| 招远市| 太保市| 泉州市| 西乌| 武隆县| 景泰县| 正镶白旗| 开鲁县| 都安| 屯昌县| 虎林市| 平原县| 诸暨市| 山丹县| 呼图壁县| 万全县| 通海县| 安国市| 泸溪县| 大姚县| 芦山县| 新竹县| 阜康市| 宁阳县| 陕西省| 温州市| 乐至县| 乳山市| 盐池县| 平邑县| 夹江县| 苗栗市| 鹤壁市| 乳山市| 和硕县| 武宣县| 崇信县| 浮山县|