Steve
Steve

Reputation: 6362

Objective-c animation block doesn't seem to fire

I have a method in my UIView subclass "Card" to dismiss an instance by fading its alpha from 1 to 0. I do a similar thing when I add the card to the superview and it fades in fine. But when I call fadeAway, it just disappears immediately. The presentation code is in my controller class. Here is my Card.h and Card.m


#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@class Card;

@interface Card : UIView {

 int            upperOperand;
    int         lowerOperand;
    NSString*   theOperator;
    int         theResult;
}

@property(nonatomic) int upperOperand;
@property(nonatomic) int lowerOperand;
@property(nonatomic, retain) NSString* theOperator;
@property(nonatomic) int theResult;

- (void)fadeAway;

@end

#import "Card.h"

@implementation Card

@synthesize upperOperand;
@synthesize lowerOperand;
@synthesize theOperator;
@synthesize theResult;

- (void)fadeAway {
    self.alpha = 1.0f;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:2.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    self.alpha = 0.0f;
    [UIView commitAnimations];  

    [self removeFromSuperview];
}

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        self.backgroundColor = [UIColor redColor];
        self.layer.cornerRadius = 15;
        self.alpha = 1.0;
        self.layer.borderColor = [[UIColor blueColor] CGColor];
        self.layer.borderWidth = 4;
    }
    return self;

   - (void)dealloc {
    [super dealloc];
}

@end

Upvotes: 0

Views: 827

Answers (1)

Kalle
Kalle

Reputation: 13346

Since you're removing self from its superview immediately, I don't think it ever has a chance to perform the animation.

You might want to look into setAnimationDidStopSelector:. There's a discussion on this here: Animation End Callback for CALayer?

Another idea is to delay the removal of the subview. For example,

[self performSelector:@selector(removeFromSuperview) 
    withObject:nil 
    afterDelay:2.0f];

Upvotes: 1

Related Questions