senty
senty

Reputation: 12857

UISlider Does Not Get Selected If Alpha is set 0 in viewDidLoad and Then Fades In

I programatically added a UISlider in my view. Also, added the function for sliderValueDidChange to observe the change on slider.

var durationSlider: UISlider!

override func viewDidLoad() {
    super.viewDidLoad()

    durationSlider = UISlider(frame:CGRectMake(0, 600, self.view.bounds.width, 20))
    durationSlider.minimumValue = 0
    durationSlider.maximumValue = 100
    durationSlider.continuous = true

 // durationSlider.alpha = 0

    durationSlider.tintColor = UIColor.redColor()
    durationSlider.value = 0
    durationSlider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
    self.view.addSubview(durationSlider)

}

func sliderValueDidChange(sender:UISlider!) {
    print("value--\(sender.value)")
}

Everything works like charm until here, and I get the value change on the UISlider perfrectly.


However, as soon as I add durationSlider.alpha = 0, and use something like this in my viewDidLoad():

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

 func handleTapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
    UIView.animateWithDuration(1, delay: 1.5, options: [], animations: {
        self.durationSlider.alpha = 1.0
    }, completion: nil)

    UIView.animateWithDuration(1.0, delay: 1.5, options: [], animations: {
        self.durationSlider.alpha = 0
    }, completion: nil)
}

At this point, I have 2 problems.

1) The UISlider is fading in instantly (without any duration or delay, as if I used alpha = 1 directly - without using animateWithDuration), whereas it fades out nicely.

2) The UISlider doesn't trigger sliderValueDidChange if I use alpha = 0 in viewDidLoad. However, if I try alpha = 1, sliderValueDidChange works as expected.


Updated Code:

This doesn't work.

func handleTapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
    UIView.animateWithDuration(0.2, delay: 0.4, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
        self.durationSlider.alpha = 1.0
    }, completion: {completed in
        UIView.animateWithDuration(2.0, delay: 2.0, options: [], animations: {
            self.durationSlider.alpha = 0
        }, completion: nil)

    })   
}

However, this works:

    UIView.animateWithDuration(0.2, delay: 0.4, options: [UIViewAnimationOptions.AllowUserInteraction], animations: {
        self.durationSlider.alpha = 1.0
    }, completion: nil)

What I am trying to achieve is a video duration controller with that UISlider, so I want to show it when the user taps on the screen with making it available to interact, and if the user doesn't interact for x seconds, hide it.

Upvotes: 0

Views: 228

Answers (2)

Raphael Oliveira
Raphael Oliveira

Reputation: 7841

I think that is the intended behaviour. Alpha 0 means the user can't see it therefore not interact with it. I saw a similar post here. Please also try to chain the animations.

 func handleTapGestureRecognizer(gestureRecognizer: UIGestureRecognizer) {
    UIView.animateWithDuration(1, delay: 1.5, options: [], animations: {
        self.durationSlider.alpha = 1.0
    }, completion:  { completed in
        UIView.animateWithDuration(1.0, delay: 1.5, options: [], animations: {
            self.durationSlider.alpha = 0
        }, completion: nil)
    }
}

Upvotes: 1

Duncan C
Duncan C

Reputation: 131471

If I remember correctly controls are disabled when their alpha is < 0.5.

Your animation code is wrong. You have both animations with the same duration and same delay. They both run at the same time, which is wrong. You need to make it so the second one uses a delay that is the sum of the delay and the duration of the previous animation. (So if the first animation has a delay of 1 and a duration of 1.5, you want to give the second animation a delay of 1.0 + 1.5 = 2.5, so it starts right after the first animation is finished.

Alternately, you can put the second animation inside the completion block for the first with a delay of 0 so it starts as soon as the first animation completes.

Upvotes: 1

Related Questions