今天是要做空畫面的處理,當所有的卡片都抽完了時候,希望能夠給使用者一個回覆,所以建立一個空畫面來處理
import UIKit
class EmptyView: UIView {
@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var tapButton: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
static func initXib() -> EmptyView {
let view = UINib(nibName: String(describing: EmptyView.self), bundle: nil).instantiate(withOwner: nil, options: nil).first as! EmptyView
return view
}
func configure(reason: EmptyViewReason) {
switch reason {
case .noBond:
self.tapButton.isHidden = true
default:
self.tapButton.isHidden = false
}
backgroundImageView.image = UIImage(named: reason.imageURL)
tapButton.text = reason.buttonText
titleLabel.text = reason.title
}
}
enum EmptyViewReason {
case noBond
case firstNoBound
case haveBond
var title: String {
switch self {
case .noBond:
return "真可惜!今天沒有緣分"
case .firstNoBound:
return "好可惜!可以再讓你說一個感人的情話試試看"
case .haveBond:
return "今日緣份到"
}
}
var buttonText: String {
switch self {
case .noBond:
return ""
case .firstNoBound:
return "說句試試看"
case .haveBond:
return ""
}
}
var imageURL: String {
switch self {
case .noBond:
return "notLucky"
case .firstNoBound:
return "again"
case .haveBond:
return "lucky"
}
}
}
之前的SlideViewController
fileprivate func removeCardAndAddNewCard(){
index += 1
let card = loadedCards.first!
card.index = index
if index == matchIndex {
matchView.configure(imageURL: data[index-1])
matchView.isHidden = false
DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: { [weak self] in
guard let self = self else { return }
self.matchView.isHidden = true
})
}
// 固定時間執行某個函數
Timer.scheduledTimer(timeInterval: 1.01, target: self, selector: #selector(enableUndoButton), userInfo: card, repeats: false)
loadedCards.remove(at: 0)
if loadedCards.isEmpty {
// 目前希望使用亂數來處理目前Bond機率
let ran = Int.random(in: 0...9)
if ran == 2 || ran == 5 || ran == 8 {
emptyView.configure(reason: .haveBond)
}
emptyView.isHidden = false
emptyView.configure(reason: .firstNoBound)
}
.
.
.
}