简体   繁体   English

UIView(不带控制器):自我消失时得到通知

[英]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.

相关问题 iPad键盘消失时如何获得通知? - How to get notified when the iPad keyboard disappears? UIView - 如何在加载视图时收到通知? - UIView - How to get notified when the view is loaded? 在加载第一个视图控制器时获得通知 - Get notified when first view controller is loaded iphone UIView - 在加载视图时收到通知? - iphone UIView - Get notified when view has loaded? 当通过手势关闭呈现的视图 controller 时如何获得通知? - How to get notified when a presented view controller is dismissed with a gesture? 轻按UITableViewCell时,UIView消失 - UIView disappears when UITableViewCell is tapped CoreData:在更改 NSManagedObject 而不保留对 NSManagedObject 的引用时获得通知 - CoreData: Get notified when NSManagedObject is changed without keeping reference to NSManagedObject 当SuperView UIScrollView滚动了UIView子类时,如何获得通知 - How can a UIView subclass get notified when it has been scrolled by its Superview UIScrollView 将UIView子类作为子视图添加到另一个视图时,如何获得通知? - How can a UIView subclass get notified when it has been added as subview to another view? 如果iOS系统弹出式窗口消失了,有没有办法得到通知? - Is there any way to get notified if an iOS system popover disappears?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM