Ios 当UIView的一个子视图正在设置动画时,如何解除锁定UIView?
-----------> 我正在尝试创建一个自定义。自定义视图的行为应该与标准视图完全相同,只是它旋转的图像看起来不同。我注意到,在以下测试代码中将自定义视图从其superview中删除时,它不会解除分配:Ios 当UIView的一个子视图正在设置动画时,如何解除锁定UIView?,ios,objective-c,cocoa-touch,uiview,memory-leaks,Ios,Objective C,Cocoa Touch,Uiview,Memory Leaks,-----------> 我正在尝试创建一个自定义。自定义视图的行为应该与标准视图完全相同,只是它旋转的图像看起来不同。我注意到,在以下测试代码中将自定义视图从其superview中删除时,它不会解除分配: ActivityIndicatorCustomView* v = [[ActivityIndicatorCustomView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 100.0f, 100.0f)]; [[UIApplication sha
ActivityIndicatorCustomView* v = [[ActivityIndicatorCustomView alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 100.0f, 100.0f)];
[[UIApplication sharedApplication].keyWindow addSubview:v];
[v removeFromSuperview];
罪魁祸首是动画块,因为当它被注释掉时,将调用dealloc。我认为这是一个保留周期,但我不知道如何解决这个问题
活动指示灯rcustomview.h
#import <UIKit/UIKit.h>
@interface ActivityIndicatorCustomView : UIView
@property(nonatomic, assign, readonly) BOOL isAnimating;
- (void)startAnimating;
- (void)stopAnimating;
@end
我认为这是因为您在完成块中继续设置动画(并在下一个[self animateWithTransform:]调用中保留
self
)。例如,请尝试检查superview以决定是否继续设置动画:
completion:^(BOOL finished) {
if (self.superview) {
[self animateWithTransform:CGAffineTransformIsIdentity(transform)
? CGAffineTransformMakeRotation((CGFloat)M_PI)
: CGAffineTransformIdentity
];
}
}
我正在跟踪一个关于块中的保留周期。它告诉我要做什么
__block MyViewController *weakSelf = self;
这是错误的。要创建弱引用,我应该这样做:
__weak ActivityIndicatorCustomView* weakSelf = self;
[UIView
animateWithDuration:ANIMATION_PERIOD_HALF_LIFE
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
weakSelf.imageView.transform = transform;
} completion:^(BOOL finished) {
[weakSelf animateWithTransform:CGAffineTransformIsIdentity(transform)
? CGAffineTransformMakeRotation((CGFloat)M_PI)
: CGAffineTransformIdentity
];
}
];
在手动参考计数下,
\u块
没有保留。在ARC下是这样的。该教程于2012年1月11日发布。我想那是在ARC时代之前。
__weak ActivityIndicatorCustomView* weakSelf = self;
[UIView
animateWithDuration:ANIMATION_PERIOD_HALF_LIFE
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
weakSelf.imageView.transform = transform;
} completion:^(BOOL finished) {
[weakSelf animateWithTransform:CGAffineTransformIsIdentity(transform)
? CGAffineTransformMakeRotation((CGFloat)M_PI)
: CGAffineTransformIdentity
];
}
];