我有一个 UIView 子类,在这个子类中我称之为另一个 UIView 子类。
星星.h
@interface Stars : UIView {
BOOL _gained;
}
@property (nonatomic, weak) id<NSObject> delegate;
@property (nonatomic) BOOL gained;
-(void)animateAndRemove;
@end
星星.m
#import "Stars.h"
@implementation Stars
@synthesize delegate = _delegate;
@synthesize gained = _gained;
- (id)initWithFrame:(CGRect)frame
{
frame.size.width = 31;
frame.size.height = 30;
self = [super initWithFrame:frame];
if (self) {
_gained = NO;
self.backgroundColor = [UIColor clearColor];
// Add star
UIImageView *star = [[UIImageView alloc] initWithFrame:frame];
[star setImage:[UIImage imageNamed:@"star-sticker"]];
[self addSubview:star];
}
return self;
}
- (void)animateAndRemove
{
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
self.transform = transform;
}
completion:^(BOOL finished){
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
self.transform = transform;
CGAffineTransform move = CGAffineTransformMakeTranslation(0, -200);
self.transform = move;
self.alpha = 0.0;
}
completion:^(BOOL finished){
if ([self.delegate respondsToSelector:@selector(removeStar:)]) {
[self.delegate performSelector:@selector(removeStar:) withObject:self];
}
}];
}];
}
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
}
@end
这只是添加了一个图像,我在删除之前对其进行了动画处理。我将其添加到原始 UIView 中,如下所示:
star1 = [[Stars alloc] init];
star1.center = self.center;
star1.delegate = self;
[self addSubview:star1];
并使用这样的动画开始删除过程:
if (CGRectIntersectsRect(thumbR, star1.frame)) {
if (!star1.gained) {
star1.gained = YES;
[star1 animateAndRemove];
}
}
完成时调用:
- (void)removeStar:(Stars *)star
{
star.delegate = nil;
[star removeFromSuperview];
star = nil;
}
现在,当类从Superview 中删除并设置为 nil 时,不会调用 dealloc 中的 NSLog。为什么不?
实际上,我在将这个 Stars 类加载到的第一个 UIView 中看到了同样的情况。当我认为它应该时,那个也没有释放。当我通过 alloc 重新分配包含 UIView 并再次对其进行初始化时,这两个 dealloc 方法都被完整调用。
当它被从视图中删除并设置为 nil 时,我期望它解除分配是错误的吗?