【问题标题】:UIVIew animation is not working properly in iOS 10UIVIew 动画在 iOS 10 中无法正常工作
【发布时间】:2026-02-10 01:00:01
【问题描述】:

GIF 显示了问题。 下面的代码显示了我如何将视图添加到超级视图中并从超级视图底部弹出。此代码在 iOS 9 中运行良好,但在 iOS 10 中运行良好。

上拉动画的动画不正确。它是从右侧出来的,而不是从底部直接出来的。

请帮我解决这个问题。

+ (ControlsView *)addArticleControlsToView:(UIView *)view bellowSubview:(UIView *)subview{
    ControlsView *articleControls = [[[NSBundle mainBundle] loadNibNamed:@"ControlsView" owner:self options:nil]firstObject];
    [view insertSubview:articleControls belowSubview:subview];
    NSDictionary *viewsDict = @{@"currentView":articleControls};
    [articleControls setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[currentView]-0-|"] options:0 metrics:@{} views:viewsDict];
    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[currentView(189)]-0-|" options:0 metrics:@{} views:viewsDict];
    [view addConstraints:horizontalConstraints];
    [view addConstraints:verticalConstraints];
    [articleControls setBackgroundColor:[UIColor clearColor]];
    articleControls.viewBottomConstraint.constant = -189;
    [articleControls layoutIfNeeded];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:articleControls action:@selector(slideSheetDown)];
    [articleControls addGestureRecognizer:tapGesture];
    return articleControls;
}

- (void)slideSheetUp {
    [UIView animateWithDuration:0.5 animations:^{
        self.viewBottomConstraint.constant = 0;
        self.scrollViewBottomConstraint.constant = 189;
        [self setBackgroundColor:[UIColor clearColor]];
        [self.scrollView.superview layoutIfNeeded];
        [self layoutIfNeeded];
    }];
}

【问题讨论】:

  • 不要更新动画块中的约束常量。
  • @Desdenova 已删除及其工作发现,但现在如何在 iOS 10 中对其进行动画处理?
  • layoutIfNeeded 位保留在块中。只是不要在其中设置常量。
  • @Desdenova 不工作

标签: objective-c xcode animation uiview ios10


【解决方案1】:

只需使用此代码

- (void)slideSheetUp {
    self.viewBottomConstraint.constant = 0;
    self.scrollViewBottomConstraint.constant = 189;
    [self setBackgroundColor:[UIColor clearColor]];
    [UIView animateWithDuration:0.5 animations:^{
        [self.scrollView.superview layoutIfNeeded];
        [self layoutIfNeeded];
    }];
}

希望它对你有用。 ;)

编码愉快!!

【讨论】:

  • 发生了什么,结果与以前相同或有任何变化?
  • 对,结果和之前一样。
  • 在应用动画之前,您的 scrollViewBottomConstraint 位置 (X) 应与初始时的 redview 相同。
【解决方案2】:

为我工作。

-(void)hideShowTimerButton:(BOOL)show{

 [UIView animateWithDuration:0.3 animations:^{
    CGRect yourViewFrame =  yourView.frame;
    if(show){
        timeContainerViewFrame.origin.y = self.view.frame.size.height;
    }else{
        timeContainerViewFrame.origin.y = self.view.frame.size.height - yourViewFrame.size.height;
    }
    yourView.frame = yourViewFrame;
} completion:^(BOOL finished) {
}];

}

【讨论】:

    【解决方案3】:

    请重新排列您的代码,如下所示,它不会对我的视图动画产生太大影响,并解决了我的问题。谢谢各位。

    - (void)slideSheetUp {
        self.viewBottomConstraint.constant = 0;
        [self setBackgroundColor:[UIColor clearColor]];
        [UIView animateWithDuration:0.5 animations:^{
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {
            self.scrollViewBottomConstraint.constant = 189;
            [self.scrollView.superview layoutIfNeeded];
        }];
    }
    

    【讨论】: