Problems animating UIView alpha
我试图将淡入淡出效果应用到我在另一个顶部以编程方式创建的UIView。
1 2 3 4 5 6 | [UIView animateWithDuration:0.5 animations:^(void) { [self.view setAlpha:0]; } completion:^(BOOL finished){ [self.view removeFromSuperview]; }]; |
完成的事件恰好在0.5秒后正确调用,但是我看不到任何褪色(我应该在底部看到UIView)。
如果我不使用Alpha,而是移开了它能正常工作的UIView(我看到了底部的UIView,而顶部的UIView滑开了),那么这似乎是与Alpha有关的问题,但我不知道出了什么问题!
1 2 3 4 5 6 7 8 | [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动画,它们通常以这种方式工作...
尝试将
值得赞扬的是汉普斯·尼尔森的评论。
1 2 3 4 5 6 7 | [UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ [self.view setAlpha:0]; } completion:^(BOOL finished) { [self.view removeFromSuperview]; }]; |
由于
我遇到同样的问题是因为线程
所以我最终在主线程中编写了动画块。
1 2 3 4 5 6 | dispatch_async(dispatch_get_main_queue(), ^{ [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{ View.alpha = 0.0f; } completion:^(BOOL finished) { }]; }); |
我遇到了确切的问题。就我而言,我希望首先隐藏视图,因此将
因此,如果要首先隐藏视图,请将其
另一个难题。在为约束设置动画时,可以在动画块外部设置新的约束常量,然后在动画内部调用
对于视图Alpha,需要直接在动画块内设置它们。
更改此:
1 2 3 4 5 6 7 8 9 10 | //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() } |
至
1 2 3 4 5 6 7 8 | //setup position changes myConstraint.constant = 10 myOtherConstraint.constant = 10 UIView.animate(withDuration: 0.25) { view.alpha = 0.5 self.view.layoutIfNeeded() } |
我遇到了完全相同的问题,没有任何建议对我有用。我改用图层不透明度解决了这个问题。
这是显示图层的代码(使用Xamarin,但您会明白的):
1 2 3 4 5 6 7 8 9 10 | //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
1 2 3 4 5 6 7 8 9 10 11 | UIView.AnimateNotify( 0.15, // duration () => { zoomView.Layer.Opacity = 0; }, (finished) => { if (finished) { zoomView.RemoveFromSuperview(); } } ); |