请查看cocoapods.org上的开始选项卡。
要在项目中使用ScratchCard,请将以下'Podfile'添加到您的项目中
pod 'ScratchCard', '~> 1.1.3'
然后运行
pod install
将ScratchView.swift和ScratchUIView.swift复制到您的项目。
然后导入选定的ScratchCard到您的文件。
import ScratchCard
class ViewController: UIViewController {
var scratchCard: ScratchUIView!
override func viewDidLoad() {
super.viewDidLoad()
scratchCard = ScratchUIView(frame: CGRect(x:50, y:80, width:320, height:480),Coupon: "image.jpg", MaskImage: "mask.png", ScratchWidth: CGFloat(40))
self.view.addSubview(scratchCard)
}
}
获取刮擦百分比非常容易。
let scratchPercent: Double = scratchCard.getScratchPercent()
处理刮擦事件(ScratchBegan、ScratchMoved和ScratchEnded)非常容易。
您可以在刮刮卡中获取刮擦位置。
请在您的代码中设置ScratchUIViewDelegate。
class ViewController: UIViewController, ScratchUIViewDelegate {
var scratchCard: ScratchUIView!
override func viewDidLoad() {
super.viewDidLoad()
scratchCard = ScratchUIView(frame: CGRect(x:50, y:80, width:320, height:480), Coupon: "image", MaskImage: "mask", ScratchWidth: CGFloat(40))
scratchCard.delegate = self
self.view.addSubview(scratchCard)
}
//Scratch Began Event(optional)
func scratchBegan(_ view: ScratchUIView) {
print("scratchBegan")
////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)
}
//Scratch Moved Event(optional)
func scratchMoved(_ view: ScratchUIView) {
let scratchPercent: Double = scratchCard.getScratchPercent()
textField.text = String(format: "%.2f", scratchPercent * 100) + "%"
print("scratchMoved")
////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)
}
//Scratch Ended Event(optional)
func scratchEnded(_ view: ScratchUIView) {
print("scratchEnded")
////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)
}
}
ScratchCard采用MIT许可。
版权所有 © 2016 Joe。