2017-06-14 19 views
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 
    } 
} 
+0

。ユーザーがあなたのアプリを終了すると、タイマーはリセットされ、ユーザーは1日に複数回ボタンを押すことができます。おそらく、再起動、終了、クラッシュが続くNSObjectなどを使用してください。 –

答えて

0

私はあなたの要件に、周りの仕事で来ています。それは最良の回避策ではありませんが、それは仕事の種類です。

  1. ボタンを1日の変更に合わせて設定します。ここでは便利な機能です:Dateオブジェクトからあなたの日付部分(2017年6月14日)を与える

    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") 
         } 
        } 
    } 
    
  2. 機能(2017年6月16日午前4時57分21秒0000)は

    /* 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) 
    } 
    
  3. ボタンの動作方法で余分な作業をしてください。ボタンを1回タップすると、残りの日は無効にする必要があるためです。

    @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) 
        } 
    } 
    
  4. 最後にviewWillAppear(_:)方法からconfigureButtonForShowingOnceInADay()を呼び出す:私はタイマーはそのための良いフィットだとは思わない

    override func viewWillAppear(_ animated: Bool) { 
        super.viewWillAppear(animated) 
        configureButtonForShowingOnceInADay() 
    }