Swift 3秒后淡出日期选择器,无反应

Swift 3秒后淡出日期选择器,无反应,swift,ios8,uipickerview,Swift,Ios8,Uipickerview,我有swift iOS8代码,它会使日期选择器淡入淡出 UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { self.PickerView.alpha = 1.0 }, completion: nil) 我想自动淡入淡出,如果3秒钟后选择器视图没有改变。可能吗 我试过这样的方法:

我有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)
        })

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

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

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,incomonent component:Int)
,则需要成为
UIPickerView
的代理


作为补充说明,按照惯例,您的变量不应以大写字母进行统计(即
self.PickerView
应为
self.PickerView
)。

不要在主线程上使用sleep()。这就是为什么你不能与UI交互,它被sleep阻塞了,而我必须使用sleep()?没有,根本不使用sleep你根本不想为此使用sleep。我正在准备答案…UIDatePicker或UIPickerView?由于选择器值更改,计时器无效后,您何时重新安排计时器?