我的UIView子类“Card”中有一个方法通过将其alpha从1淡化为0来解除实例。当我将卡添加到superview时,我做了类似的事情并且它很好地消失了。但是当我打电话给fadeAway时,它会立即消失。演示代码在我的控制器类中。这是我的Card.h和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
答案 0 :(得分:1)
由于您立即从其超级视图中删除self
,我认为它没有机会执行动画。
您可能想要查看setAnimationDidStopSelector:
。这里有一个讨论:Animation End Callback for CALayer?
另一个想法是延迟删除子视图。例如,
[self performSelector:@selector(removeFromSuperview)
withObject:nil
afterDelay:2.0f];