2010-07-08 73 views
0

我在我的UIView子类“Card”中有一个方法,通过将其alpha从1褪为0来消除一个实例。我将类似的东西添加到超级观点,它会淡化。但是当我打电话给fadeAway时,它会立即消失。演示文稿代码位于我的控制器类中。这是我的Card.h和Card.mObjective-c动画块似乎不会触发


#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 

回答

1

既然你从它的父立即删除self,我不认为它曾经有机会执行动画。您可能需要查看setAnimationDidStopSelector:。这里有一个讨论:Animation End Callback for CALayer?

另一个想法是推迟子视图的删除。例如,

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

你说,[UIView的commitAnimations]行只是_starts_动画,它马上就转到下一行并将其删除?这对我来说很有意义。我已经看到setAnimationDidStopSelector:之前,但还没有尝试过使用它。我认为这意味着另一种方法 - 一个做动画,一个完成后删除视图,对吗? – Steve 2010-07-08 16:04:17

+0

我认为卡勒的回答是正确的,推迟清理到animationDidStopselector :. – Elfred 2010-07-08 16:10:37

+0

没错。那么,或者您可以延迟与动画匹配的延迟。我更新了上面的答案以包含此方法的示例。 – Kalle 2010-07-08 17:15:33