2010-07-08 51 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