有人可以帮助我将此功能添加到我的代码中吗?
我需要一个位于视图控制器顶部的计时器,该计时器与被点击的按钮相关。这个计时器只允许按钮每天推一次。我怎么能这样做?
这是我的视图控制器代码。
示例代码
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
}
}
答案 0 :(得分:0)
我已经满足您的要求。虽然这不是最好的解决办法,但它确实能起作用。
根据日期变化配置您的按钮。这是一个方便的功能:
func configureButtonForShowingOnceInADay() {
spinButton.tintColor = #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1)
let now = Date()
var savedDay: Date?
/* Check if the app launched for first time or not */
let appLanuchedBefore = UserDefaults.standard.bool(forKey: "ApplicationDidLaunchPreviously")
if !appLanuchedBefore {
// App launched for first time, so Mark the app as launched in the UserDefaults
UserDefaults.standard.set(true, forKey: "ApplicationDidLaunchPreviously")
// When App launched for first time, it is must to have a date before today's date.
// A previous date is required for the button to be enabled for the first time
savedDay = Calendar.current.date(byAdding: .day, value: -1, to: now)
} else {
// App launched multiple times. So read the date from UserDefaults
savedDay = UserDefaults.standard.object(forKey: "date") as? Date
}
// get only the Date portion from now, not with Time
let today = todaysDate(date: now)
if let savedDate = savedDay {
if todaysDate(date: savedDate) == today {
// Saved date in UserDefaults matches with today. So the button should not be enabled
spinButton.isUserInteractionEnabled = false
spinButton.tintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
} else {
// Saved date in UserDefaults doesn't match with today. So the button should be enabled
spinButton.isUserInteractionEnabled = true
spinButton.tintColor = #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1)
UserDefaults.standard.set(now, forKey: "date")
}
}
}
从Date对象(2017-06-16 04:57:21 +0000)为您提供日期部分(2017年6月14日)的函数
/* A method for converting Date-with-Time to Date-only String */
func todaysDate(date: Date)-> String {
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .none
dateFormatter.dateStyle = .long
return dateFormatter.string(from: date)
}
在Button的操作方法中做额外的工作。因为如果按钮被轻敲一次,则应该在当天的其余时间禁用该按钮。
@IBAction func spinButtonTapped() {
animateSpinButton()
// Pick 3 random emojis
for i in 0...2 {
selectEmojiPickerComponentRow(Int(arc4random_uniform(99)), component: i)
}
showFeedbackForWinningSpin()
// If the button is Tapped then toggle the button's user interaction property
sender.isUserInteractionEnabled = !sender.isUserInteractionEnabled
if sender.isUserInteractionEnabled {
sender.tintColor = #colorLiteral(red: 0, green: 0.4784313725, blue: 1, alpha: 1)
} else {
sender.tintColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
}
}
最后从configureButtonForShowingOnceInADay()
方法调用viewWillAppear(_:)
:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configureButtonForShowingOnceInADay()
}