user3746428
user3746428

Reputation: 11175

Animating alpha to 0 doesn't function properly

I have a couple of animations in my app that involve changing the alpha value of different objects. These work great for fading the object in, but they never seem to work for fading to 0.

        UIView.animateWithDuration(0.4,
            delay: 0,
            options: .CurveLinear & .AllowUserInteraction & .BeginFromCurrentState,
            animations: {
                cell.notesLabel.alpha = 0
            }, completion: nil)

Basically, the transparency just switches straight for 100% to 0% instantly. If I increase the duration, it just takes longer to start the animation and then it does it instantly again.

Anyone have any ideas?

Entire Code:

let cell = tableView.cellForRowAtIndexPath(indexPath) as CustomTransactionTableViewCell
        if cell.notesLabel.alpha == 100 {
            UIView.animateWithDuration(0.4,
                delay: 0,
                options: .CurveLinear | .AllowUserInteraction | .BeginFromCurrentState,
                animations: {
                    cell.notesLabel.alpha = 0
                }, completion: { (finished:Bool) in
                    UIView.animateWithDuration(1,
                        delay: 0,
                        options: .CurveLinear & .AllowUserInteraction & .BeginFromCurrentState,
                        animations: {
                            cell.paymentArrowImage.frame.origin.x = cell.paymentArrowImage.frame.origin.x - 400
                            cell.creditArrowImage.frame.origin.x = cell.creditArrowImage.frame.origin.x - 400
                            cell.paymentNameLabel.frame.origin.x = cell.paymentNameLabel.frame.origin.x + 400
                            cell.dateLabel.frame.origin.x = cell.dateLabel.frame.origin.x + 400
                            cell.costLabel.frame.origin.x = cell.costLabel.frame.origin.x - 400
                        }, completion: nil)
            })
        } else {
            UIView.animateWithDuration(0.4,
                delay: 0,
                options: .CurveLinear & .AllowUserInteraction & .BeginFromCurrentState,
                animations: {
                    cell.paymentArrowImage.frame.origin.x = cell.paymentArrowImage.frame.origin.x + 400
                    cell.creditArrowImage.frame.origin.x = cell.creditArrowImage.frame.origin.x + 400
                    cell.costLabel.frame.origin.x = cell.costLabel.frame.origin.x + 400
                    cell.paymentNameLabel.frame.origin.x = cell.paymentNameLabel.frame.origin.x - 400
                    cell.dateLabel.frame.origin.x = cell.dateLabel.frame.origin.x - 400
                    cell.notesLabel.alpha = 100
                }, completion: nil)
        }

Upvotes: 1

Views: 257

Answers (2)

Mick MacCallum
Mick MacCallum

Reputation: 130222

The problem is that you're using 0 to 100 as your alpha values, when they are expected to be values from 0.0 to 1.0. This is causing your condition to malfunction as well as causing the animation issues.

Upvotes: 2

erdekhayser
erdekhayser

Reputation: 6657

Your options currently are .CurveLinear & .AllowUserInteraction & .BeginFromCurrentState

These options are actually bit-shifted values, so they are represented by 001, or 010, or 100, and so on.

When you & them together, you are actually testing to see which bits they all share in common. 001 % 010 returns 00, because where a 1 is found in one, a 0 is in the either.

What you want is to | (or) them together. This makes it so that if a 1 is found in that place value for any of the numbers, it is found in the answer. For example, 010 | 001 returns 011.

For your code, you really want .CurveLinear | .AllowUserInteraction | .BeginFromCurrentState.

Upvotes: 2

Related Questions