繁体   English   中英

动画UIView alpha的问题

[英]Problems animating UIView alpha

我正在尝试将淡入淡出应用于我在另一个上面以编程方式创建的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动画,它们通常以这种方式工作......

尝试明确地将opaque设置为NO 我有同样的问题和设置解决了我的问题。 显然,不透明的视图似乎不适合alpha。

归功于Hampus Nilsson的评论。

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

这样做是正确的,因为options:UIViewAnimationOptionCurveEaseInOut ,你的褪色会更好。

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

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

我有完全相同的问题,没有一个建议对我有用。 我通过使用图层不透明度来克服这个问题。 这是显示图层的代码(使用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();
                    }
                }
            );

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

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

还有一块拼图。 当您设置动画约束时,可以在动画块之外设置新约束常量,然后在动画中调用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()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM