3 秒后没有反应淡出日期选择器

fade out date picker after 3 seconds without reaction

我有 swift iOS8 代码,它会淡入日期选择器。

  UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.PickerView.alpha = 1.0
            }, completion: nil)

我想自动淡出它,如果 3 秒后选择器视图没有改变。这可能吗?

我试过这样的事情:

        // Fade in
        UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
            self.PickerView.alpha = 1.0
            }, completion: { finished in
                sleep(3)
                UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
                    self.PickerView.alpha = 0.0
                    }, completion: nil)
        })

问题是:睡眠处于活动状态时我无法更改选择器的值。

您需要设置一个计时器,用于在选择器淡出视图时触发。您将使该计时器无效 if/when 选择器值更改:

var timer: NSTimer?

override func viewDidLoad() {
    super.viewDidLoad()

    // Fade the picker in
    UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
        self.PickerView.alpha = 1.0
    }) { (finished) -> Void in

        // Start the timer after the fade-in has finished
        self.startTimer()

    }
}

func startTimer() {
    self.timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: "fadeOutPicker", userInfo: nil, repeats: false)
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    // Invalidate the timer when the picker value changes
    timer?.invalidate()

    // (Re)start the timer
    startTimer()
}

func fadeOutPicker() {
    // Fade the picker out
    UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
        self.PickerView.alpha = 0.0
    }, completion: nil)
}

如果 pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 没有被调用,您将需要成为 UIPickerView 的代表。

附带说明一下,按照惯例,您的变量不应使用大写字母统计(即 self.PickerView 应为 self.pickerView)。