[英]UIView (without controller) : get notified when self disappears
I have a UIView without any associated UIViewController constantly animating with auto layout and layoutIfNeeded (see code bellow). 我有一个没有任何关联的UIViewController的UIView,它始终使用自动布局和layoutIfNeeded进行动画处理(请参见下面的代码)。 But when this view (which is contained in another view) disappears (for example when a modal view covers the view it is contained in) and after I dismiss this modal view, the animating view isn't animating anymore.
但是,当该视图(包含在另一个视图中)消失时(例如,当模态视图覆盖了它所包含的视图时),并且在我关闭了该模态视图之后,动画视图不再进行动画处理。
I managed to animate it back with the didMoveToWindow:animated method but i'm not sure this is the proper approach. 我设法用didMoveToWindow:animated方法对其进行了动画处理,但是我不确定这是否是正确的方法。
@interface AnimatingView()
@property (strong, nonatomic) UIView *aSubview;
@property (strong, nonatomic) NSLayoutConstraint *constraintTop;
@property (assign, nonatomic, getter = isStarted) BOOL started;
@property (assign, nonatomic) BOOL stopAnimation;
@end
@implementation AnimatingView
- (id)init
{
self = [super init];
if (self) {
self.stopAnimation = YES;
/* setting base auto layout constraints */
}
return self;
}
-(void)animate{
float aNewConstant = arc4random_uniform(self.frame.size.height);
[UIView animateWithDuration:ANIMATION_DURATION animations:^{
[self removeConstraint:self.constraintTop];
self.constraintTop = [NSLayoutConstraint constraintWithItem:self.aSubview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:aNewConstant];
[self addConstraint:self.constraintTop];
[self layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished && !self.stopAnimation) {
[self animate];
}
}];
}
- (void)didMoveToWindow{
[super didMoveToWindow];
if ([self isStarted]) {
[self stop];
[self start];
}
}
-(void)start{
if (![self isStarted]) {
self.stopAnimation = NO;
[self setStarted:YES];
[self animate];
}
}
-(void)stop{
if ([self isStarted]) {
self.stopAnimation = YES;
[self setStarted:NO];
}
}
@end
You can always use a behavioral pattern : KVO to control the state of the UIView or what I would do, use a notification to animate the view when you want to. 您始终可以使用行为模式:KVO来控制UIView的状态或我将要执行的操作,并在需要时使用通知为视图添加动画。 Both options are valid.
这两个选项均有效。 You can have as well a delegate associated with this class and implement the behavior you want in your UIView.
您还可以拥有与此类关联的委托,并在UIView中实现所需的行为。
Assuming your view is contained in another view which is contained in a Controller, you can set a BOOL variable called didReturnFromModalChildView
which you set it true every time you present that modal view. 假设您的视图包含在Controller包含的另一个视图中,则可以设置一个名为
didReturnFromModalChildView
的BOOL变量,每次您显示该模式视图时,将其设置为true。 When you dismiss that modal view, your application will call -(void)viewWillAppear:(BOOL)animated
. 当您关闭该模式视图时,您的应用程序将调用
-(void)viewWillAppear:(BOOL)animated
。 Here you have to check if that BOOL variable is set to Yes and then use a public method calling if you have a reference to the view you want to animate, or a delegation process or observer-notification pattern. 在这里,您必须检查BOOL变量是否设置为“是”,然后使用公共方法调用,如果您具有要创建动画的视图的引用,或者是委托过程或观察者通知模式。 Don't forget to set didReturnFromModalChildView to false.
不要忘记将didReturnFromModalChildView设置为false。
I hope this will help you. 我希望这能帮到您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.