0
誰かがこの機能を自分のコードに追加できますか?ボタン機能付きタイマー?
タップされているボタンに関連するビューコントローラの上部にタイマーが必要です。このタイマーは、ボタンを1日に1回だけ押すことができます。どうすればこのことができますか?
ここに私のView Controllerコードがあります。
サンプルコードは
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet var emojiPickerView: UIPickerView!
@IBOutlet var spinButton: UIButton!
var emojiArray = ["", "", "", "", "", "", "", "", "", ""]
var reelArray0 = [Int]()
var reelArray1 = [Int]()
var reelArray2 = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
emojiPickerView.delegate = self
emojiPickerView.dataSource = self
setUpUI()
addRandomNumbersToReelArrays()
// Set reels to middle positions
for i in 0...2 {
selectEmojiPickerComponentRow(50, component: i)
}
}
func setUpUI() {
spinButton.layer.cornerRadius = 15
spinButton.layer.shadowColor = UIColor.black.cgColor
spinButton.layer.shadowOffset = CGSize(width: 0.0, height: 4.0)
spinButton.layer.shadowOpacity = 0.5
spinButton.layer.shadowRadius = 5.0
}
@IBAction func spinButtonTapped() {
animateSpinButton()
// Pick 3 random emojis
for i in 0...2 {
selectEmojiPickerComponentRow(Int(arc4random_uniform(99)), component: i)
}
showFeedbackForWinningSpin()
}
func showFeedbackForWinningSpin() {
if(reelArray0[emojiPickerView.selectedRow(inComponent: 0)] == reelArray1[emojiPickerView.selectedRow(inComponent: 1)] && reelArray1[emojiPickerView.selectedRow(inComponent: 1)] == reelArray2[emojiPickerView.selectedRow(inComponent: 2)]) {
// JACKPOT
spinButton.isEnabled = false
delay(0.8) {
self.showAlertForJackpot()
self.spinButton.isEnabled = true
}
} else if ((reelArray0[emojiPickerView.selectedRow(inComponent: 0)] == reelArray1[emojiPickerView.selectedRow(inComponent: 1)]
|| reelArray1[emojiPickerView.selectedRow(inComponent: 1)] == reelArray2[emojiPickerView.selectedRow(inComponent: 2)])
|| (reelArray0[emojiPickerView.selectedRow(inComponent: 0)] == reelArray2[emojiPickerView.selectedRow(inComponent: 2)])) {
// Two emojis matched
spinButton.isEnabled = false
delay(0.8) {
self.showAlertForWin()
self.spinButton.isEnabled = true
}
}
}
func showAlertForJackpot() {
let alert = UIAlertController(title: "JACKPOT!",
message: "Congratulations! You won the jackpot!",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Woohoo!", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func showAlertForWin() {
let alert = UIAlertController(title: "YOU WON!",
message: "Congratulations! \nYou got 2 matching emojis! \n\nCan you hit the JACKPOT next time?",
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Yes I can!", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func animateSpinButton() {
UIView.animate(withDuration: 0.1,
animations: {
self.spinButton.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
self.spinButton.layer.shadowRadius = 3.0
},
completion: { finish in
UIView.animate(withDuration: 0.1, animations: {
self.spinButton.transform = CGAffineTransform.identity
self.spinButton.layer.shadowRadius = 5.0
})
})
}
func selectEmojiPickerComponentRow(_ row: Int, component: Int) {
emojiPickerView.selectRow(row, inComponent: component, animated: true)
}
func addRandomNumbersToReelArrays() {
for _ in 0..<100 {
reelArray0.append(Int(arc4random_uniform(10)))
reelArray1.append(Int(arc4random_uniform(10)))
reelArray2.append(Int(arc4random_uniform(10)))
}
}
func delay(_ delay: Double, closure: @escaping()->()) {
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC)))/Double(NSEC_PER_SEC),
execute: closure
)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 100
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return 100.0
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 100.0
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel()
if component == 0 {
pickerLabel.text = emojiArray[reelArray0[row]]
} else if component == 1 {
pickerLabel.text = emojiArray[reelArray1[row]]
} else {
pickerLabel.text = emojiArray[reelArray2[row]]
}
pickerLabel.font = UIFont(name: "Apple Color Emoji", size: 80)
pickerLabel.textAlignment = NSTextAlignment.center
return pickerLabel
}
}
。ユーザーがあなたのアプリを終了すると、タイマーはリセットされ、ユーザーは1日に複数回ボタンを押すことができます。おそらく、再起動、終了、クラッシュが続くNSObjectなどを使用してください。 –