首页 文章

动画UIView alpha的问题

提问于
浏览
5

我正在尝试将淡入淡出应用于我在另一个上面以编程方式创建的UIView .

[UIView animateWithDuration:0.5 animations:^(void) {
    [self.view setAlpha:0];
}
                 completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

完成的事件在0.5秒之后被正确调用,但我没有看到任何淡入淡出(我应该看到底部的UIView) .

如果不使用alpha,我会移开UIView它可以工作(我看到底部UIView,而顶部UIView滑落),所以它似乎是一个与alpha相关的问题,但我无法弄清楚什么是错的!

[UIView animateWithDuration:0.5 animations:^(void) {
    CGRect o = self.view.frame;
    o.origin.x = 320;
    self.view.frame = o;
}
                 completion:^(BOOL finished){
                     [self.view removeFromSuperview];
                 }];

我之前使用过alpha动画,它们通常以这种方式工作......

6 回答

  • 1

    尝试明确地将 opaque 设置为 NO . 我有同样的问题和设置解决了我的问题 . 显然,不透明的视图似乎不能很好地与alpha一起使用 .

    归功于Hampus Nilsson的评论 .

  • 0
    [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut 
    animations:^{
        [self.view setAlpha:0];
    } 
    completion:^(BOOL finished) {
        [self.view removeFromSuperview];
    }];
    

    这将正常工作,因为 options:UIViewAnimationOptionCurveEaseInOut ,你的褪色会更好 .

  • 0

    我遇到了确切的问题 . 在我的情况下,我希望首先隐藏视图,所以我将 hidden 设置为 true . 我想更改 alpha 值会自动更改 hidden 属性,但事实并非如此 .

    因此,如果您希望首先隐藏视图,请将其 alpha 设置为 0 .

  • 6

    我有完全相同的问题,没有一个建议对我有用 . 我通过使用图层不透明度来克服这个问题 . 这是显示图层的代码(使用Xamarin,但你会得到这个想法):

    //Enter the view fading
            zoomView.Layer.Opacity = 0;
    
            UIApplication.SharedApplication.KeyWindow.RootViewController.View.Add(zoomView);
    
            UIView.AnimateNotify(
                0.15, // duration
                () => { zoomView.Layer.Opacity = 1.0f },
                (finished) => { }
            );
    

    这是为了淡出相同的zoomView

    UIView.AnimateNotify(
                    0.15, // duration
                    () => { zoomView.Layer.Opacity = 0; },
                    (finished) =>
                    {
                        if (finished)
                        {
                            zoomView.RemoveFromSuperview();
                        }
                    }
                );
    
  • 2

    我在我的情况下遇到同样的问题因为线程所以我最终在主线程中写了动画块 .

    dispatch_async(dispatch_get_main_queue(), ^{        
        [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            View.alpha = 0.0f;
        } completion:^(BOOL finished) {
        }];
    });
    
  • 0

    还有一块拼图 . 当您设置动画约束时,可以在动画块之外设置新约束常量,然后在动画中调用 layoutIfNeeded .

    对于视图alphas,这些需要直接在动画块内设置 .

    改变这个:

    //setup position changes
    myConstraint.constant = 10
    myOtherConstraint.constant = 10
    //whoops, alpha animates immediately
    view.alpha = 0.5
    
    UIView.animate(withDuration: 0.25) {
       //views positions are animating, but alpha is not   
       self.view.layoutIfNeeded()
    }
    

    //setup position changes
    myConstraint.constant = 10
    myOtherConstraint.constant = 10
    
    UIView.animate(withDuration: 0.25) {
       view.alpha = 0.5
       self.view.layoutIfNeeded()
    }
    

相关问题