【问题标题】:Removing a view from it's superview after animation repositions other views在动画重新定位其他视图后从其父视图中删除视图
【发布时间】:2014-03-21 11:54:38
【问题描述】:

我的应用模拟纸牌游戏。我的控制器中有一个名为 gridView 的视图,其中包含 16 个卡片视图。卡片视图已经定位在网格视图中(它是绿色的),没有任何布局约束:

现在在游戏的某个时刻,我需要为两个卡片视图设置动画,将它们移到屏幕边界之外,并且在动画完成后,我需要将它们从它的超级视图(网格视图)中移除。但是我不需要同时执行此操作,而是每次执行一个视图,因此我使用了一个动画,当完成时它会触发另一个视图。

我使用两个数组:cardViews 包含网格视图内的所有卡片视图,但不包含需要用动画删除的卡片视图,它们包含在 matchedCardViews 中。所以我写了这个方法:

- (void) animateMatchedCardViews {
    if(self.matchedCardViews.count) {
        UIView* view= self.matchedCardViews.firstObject;
        [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            view.frame= aFrameOutOfScreenBounds;  // I checked out: the frame is computed
                                                  // correctly, converted in grid view's
                                                  // coordinates
        } completion:^(BOOL finished) {
            [view removeFromSuperview];
            [self.matchedCardViews removeObject:view];
            // I recursively call the method to remove the other card
            // in self.matchedCardViews. 
            [self animateMatchedCardViews];
        }];
    }
}

但是从它的超级视图中删除视图会搞砸一切:第一个动画运行良好,我看到视图慢慢地走出屏幕,但第二个动画完全错误:所有其他视图我不知道为什么是布局再次,在错误的位置,并且应该动画的第二个卡片视图不会移动到屏幕外的点,而是网格视图内。红圈中的视图是移动到错误位置的视图:

PS:如果我不调用 [view removeFromSuperview] 行所有修复,但我需要将其从超级视图中删除。

【问题讨论】:

  • 约束的作用,禁用自动布局,一切都会正常工作
  • 您是否在[view removeFromSuperview] 之后删除了matchedCardViews 的视图?
  • @Basheer_CAD 你是对的。但我需要视图控制器中其他视图的自动布局。网格视图内的视图没有任何约束。即使使用自动布局,有没有办法让它工作?
  • 是的@KudoCC,但我没有包括这一行来简化代码(真的我使用三个数组)。
  • 添加图像表单 ui builder,显示这些卡片的约束

标签: ios objective-c animation


【解决方案1】:

试试这个,

- (void) animateMatchedCardViews {
    if(self.matchedCardViews.count) {
        UIView* view= self.matchedCardViews.firstObject;
        [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            view.frame= aFrameOutOfScreenBounds;  // I checked out: the frame is computed
                                              // correctly, converted in grid view's
                                              // coordinates
        } completion:^(BOOL finished) {
           if (finished) {
               [view removeFromSuperview];
               [self.matchedCardViews removeObject:view];
               // I recursively call the method to remove the other card
               // in self.matchedCardViews. 
               [self animateMatchedCardViews];
           }

        }];
    }
}

【讨论】:

  • 对不起,我已经这样做了。我只是忘记将这一行粘贴到问题代码中。我现在修好了。
【解决方案2】:

如果我使用自动布局,xcode 似乎想自动向未指定约束的视图添加约束:

这样做可能是为了解决歧义,但在我的情况下没有任何歧义,因为我手动将视图定位在网格视图内。

我找到的解决方案是继承UIView 并覆盖addConstraint:addConstraints:,而不调用超类实现,这样就不会真正添加约束。这可以完美运行,没有任何崩溃。

- (void) addConstraint:(NSLayoutConstraint *)constraint {
}

- (void) addConstraints:(NSArray *)constraints {
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2012-09-02
    相关资源
    最近更新 更多