启用用户交互的 FadeIn 和 FadeOut UISlider

FadeIn & FadeOut UISlider with UserInteraction Enabled

我使用 UISlider 创建了一个视频持续时间线。效果很好;我可以获得滑块值更改。然后,我尝试向其添加视觉效果(淡入和淡出),因此当用户点击屏幕时,滑块淡入,如果没有其他交互则淡出。

为了实现淡入淡出的效果,我把alpha调到0-1之间。但是,ios 显然不接受 alpha 0.5 以下元素的用户交互。因此,我无法用普通的 animateWithDuration 实现这一点,因此我尝试添加一个计算秒数的计数器。

(更新: 很明显,我想要实现的是,我有一个全屏视频,我想放置一个滑块来显示视频时间(当前时间+持续时间)。我想将滑块最初设置为不可见,当用户点击屏幕时,滑块变得可见。然后,用户在滑块上进行交互。但是,我想让滑块消失自上次点击后 5 秒后退出。)

我试过的...

 var durationSlider: UISlider!
 var timer = NSTimer()
 var counter = 0

 override func viewDidLoad() {
    super.viewDidLoad()

    durationSlider.minimumValue = 0
    durationSlider.maximumValue = 100
    durationSlider.continuous = true
    durationSlider.alpha = 0 
    durationSlider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
    self.view.addSubview(durationSlider)

    let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGestureRecognizer:")
    tapGestureRecognizer.numberOfTapsRequired = 1
    self.player.view.addGestureRecognizer(tapGestureRecognizer)
 }

 func scheduledTimerWithTimeInterval(){
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounting"), userInfo: nil, repeats: true)
 }

 func updateCounting(){
    counter = counter + 1
 }

到这里为止,一切正常,如果我只使用淡入甚至不使用计数器,它就可以工作。

func handleTapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
        UIView.animateWithDuration(0.2, delay: 0.2, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
        self.durationSlider.alpha = 1.0
        print("B")

    }, completion: nil)
}

这里开始问题

如果我尝试在完成中使用淡出,它会直接进入完成闭包,所以它甚至不会给我一秒钟的时间来与滑块(alpha < 5)交互。

    UIView.animateWithDuration(0.2, delay: 0.4, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
        self.durationSlider.alpha = 1.0
        print("B")

    }, completion: {completed in
        UIView.animateWithDuration(2.0, delay: 10.0, options: [], animations: {
            print("A")
            self.durationSlider.alpha = 0
        }, completion: nil)

    })

当然,也尝试将它们分成两个单独的函数,但这会更迅速地改变打印日志(所以 alpha):

    UIView.animateWithDuration(0.2, delay: 0.4, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
        self.durationSlider.alpha = 1.0
        print("B")

    }, completion: nil)

    UIView.animateWithDuration(2.0, delay: 10.0, options: [], animations: {
        print("A")
        self.durationSlider.alpha = 0
    }, completion: nil)

最后,我突然想到了设置计时器的想法,所以我尝试了类似的方法,但也不走运。它启动计数器并且它正在上升,但我认为这个逻辑在这里不起作用,因为它永远不会进入那个 if 条件。

 func handleTapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
    counter = 0
    scheduledTimerWithTimeInterval()

    UIView.animateWithDuration(0.2, delay: 0.2, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
        self.durationSlider.alpha = 1.0
        print("B")

    }, completion: nil)

    if counter > 6 {
        UIView.animateWithDuration(2.0, delay: 5.0, options: [], animations: {
            print("A")
            self.durationSlider.alpha = 0
        }, completion: nil)
    }

实现我想要实现的目标的最佳方式是什么?通过在 UISlider 上启用用户交互来调整淡入和淡出的正确方法是什么?

这些简单的步骤就可以做到:

  1. 当用户点击以显示滑块时,设置一个 5 秒的计时器(并以相当快的速度设置滑块的淡入动画,例如 0.3 秒)。 [一个不错的变化可能是淡入滑块并在动画的完成处理程序中启动计时器。]

  2. 在滑块的 Value Changed 操作中,使计时器无效/取消并创建一个新的五秒计时器。 (如果用户从不更改滑块,则不会发生此步骤。但这没关系;已经有一个计时器 运行。)

  3. 无论如何,你有一个计时器 运行,它最终会触发(从你淡入后五秒,或者从用户上次更改后五秒滑块)。当计时器触发时,淡出滑块。全部完成!