倉庫地址,喜歡就star一下❤️html
這是一個簡單卻功能強大的刮刮樂視圖,幾行代碼就能夠實現刮刮樂效果,並且性能良好。下面有美女福利喲,相信我,你會喜歡的😍😍ios
相信你們都買過彩票刮刮樂,老是會抱着中大獎的狀況去刮,但願本身是最幸運的那一個,刮中五百萬,抱得美人歸,今後走上人生巔峯。但現實每每是你口袋裏面的幾十塊零錢,幾分鐘就被消費殆盡了😂 許多APP也集成了這一功能,好比用支付寶線下支付後就有刮刮樂。雖然刮中的都是些沒多大用的優惠券,但老是會吸引人去刮一刮,萬一中了大獎呢😎git
多說無益,先來看看實現的效果吧github
參照了一個叫作「撕掉她的衣服」APP,效果很是sexy,有沒有一種心跳加快,血脈膨脹的感受。(相信你們火燒眉毛想要體驗一下了,點擊fir.im/JXScratchVi…,經過safari打開該連接,安裝以後信任證書,就能夠快速體驗了)相信我在空白處,雙擊一下,你會發現新大陸😍swift
在網上搜索了一番,方案基本上就是這種:連接。 核心代碼:ide
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 觸摸任意位置
UITouch *touch = touches.anyObject;
// 觸摸位置在圖片上的座標
CGPoint cententPoint = [touch locationInView:self.imageView];
// 設置清除點的大小
CGRect rect = CGRectMake(cententPoint.x, cententPoint.y, 20, 20);
// 默認是去建立一個透明的視圖
UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);
// 獲取上下文(畫板)
CGContextRef ref = UIGraphicsGetCurrentContext();
// 把imageView的layer映射到上下文中
[self.imageView.layer renderInContext:ref];
// 清除劃過的區域
CGContextClearRect(ref, rect);
// 獲取圖片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 結束圖片的畫板, (意味着圖片在上下文中消失)
UIGraphicsEndImageContext();
self.imageView.image = image;
}
複製代碼
缺點很明顯: 一、畫筆是矩形的,看着很難受; 二、繪畫的核心代碼用了CoreGraphics,每次移動都要從新開啓一個context上下文,繪製image,對CPU的性能有很大的消耗。點擊該連接,詳細瞭解CAShapeLayer比CG的優點 因此,我想了一個騷技巧,用CAShapeLayer做爲mask來實現該效果。點擊該連接,瞭解mask圖層遮罩性能
/// 指定初始化器
///
/// - Parameters:
/// - contentView: 內容視圖,好比彩票的獎品詳情內容。(須要隱藏起來的內容)
/// - maskView: 遮罩視圖
public init(contentView: UIView, maskView: UIView) {
super.init(frame: CGRect.zero)
scratchMaskView = maskView
self.addSubview(scratchMaskView)
scratchContentView = contentView
self.addSubview(scratchContentView)
maskLayer = CAShapeLayer()
maskLayer.strokeColor = UIColor.red.cgColor
maskLayer.lineWidth = strokeLineWidth
maskLayer.lineCap = strokeLineCap
scratchContentView?.layer.mask = maskLayer
maskPath = UIBezierPath()
}
複製代碼
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let point = touch.location(in: scratchContentView)
maskPath.move(to: point)
}
open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {
return
}
let point = touch.location(in: scratchContentView)
maskPath.addLine(to: point)
maskPath.move(to: point)
maskLayer.path = maskPath.cgPath
}
複製代碼
//獲取透明像素佔總像素的百分比
private func getAlphaPixelPercent(img: UIImage) -> Float {
//計算像素總個數
let width = Int(img.size.width)
let height = Int(img.size.height)
let bitmapByteCount = width * height
//獲得全部像素數據
let pixelData = UnsafeMutablePointer<UInt8>.allocate(capacity: bitmapByteCount)
let colorSpace = CGColorSpaceCreateDeviceGray()
let context = CGContext(data: pixelData,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: width,
space: colorSpace,
bitmapInfo: CGBitmapInfo(rawValue:
CGImageAlphaInfo.alphaOnly.rawValue).rawValue)!
let rect = CGRect(x: 0, y: 0, width: width, height: height)
context.clear(rect)
context.draw(img.cgImage!, in: rect)
//計算透明像素個數
var alphaPixelCount = 0
for x in 0...Int(width) {
for y in 0...Int(height) {
if pixelData[y * width + x] == 0 {
alphaPixelCount += 1
}
}
}
free(pixelData)
return Float(alphaPixelCount) / Float(bitmapByteCount)
}
//展現所有
open func showContentView() {
self.scratchContentView.layer.mask = nil
}
複製代碼
let contentView = UILabel()
contentView.backgroundColor = UIColor.white
contentView.textAlignment = .center
contentView.font = UIFont.systemFont(ofSize: 25)
contentView.text = "恭喜你刮中500萬"
contentView.numberOfLines = 0
let maskView = UIView()
maskView.backgroundColor = UIColor.lightGray
let ratio = self.bounds.size.width/400
scratchView = JXScratchView(contentView: contentView, maskView: maskView)
scratchView.delegate = self
scratchView.strokeLineWidth = 25
scratchView.strokeLineCap = kCALineCapRound
scratchView.frame = CGRect(x: 33*ratio, y: 140*ratio, width: 337*ratio, height: 154*ratio)
addSubview(scratchView)
複製代碼
public init(contentView: UIView, maskView: UIView)
初始化器,只須要傳入UIView及其子類就能夠了。strokeLineCap
屬性設置stroke形狀,默認kCALineCapRound
strokeLineWidth
屬性設置stroke線寬,默認20JXScratchViewDelegate
,實現func scratchView(scratchView: JXScratchView, didScratched percent: Float)
代理方法,就能夠實時獲取刮刮樂的百分比。JXScratchTicketView