淡出除一个特定子视图之外的子视图

时间:2013-09-15 22:53:39

标签: ios uiview fadeout

我试图淡出整个视图,但只有一个特定的subview,这会强调subview的外观更清晰。假设self.view有5 textfields和2 UIViewsubviews,我希望强调1 UIView。有没有办法淡出/在整个self.view中使用5 textfields和1 UIView而不触及强调的UIView

我已经尝试过使用方法

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

,例如:

[UIView animateWithDuration:0.75
                 animations:^{
                     self.view.alpha = 0.5f;

                 }
                 completion:^(BOOL finished){self.view.layer.shouldRasterize = NO;}];

但是,似乎整个子视图都受到上面UIView动画的影响。任何建议或提示将不胜感激。

1 个答案:

答案 0 :(得分:7)

如果降低UIView的alpha值,则会降低所有子视图的alpha值。解决问题的方法是单独降低每个子视图的alpha值,除了你想要的那个:

UIView emphasizedView = ...

[UIView animateWithDuration:0.75
                 animations:^{
                     for (UIView *view in self.view.subviews) {
                         if (view != emphasizedView) {
                             view.alpha = 0.5f;
                         } else {
                             view.alpha = 1;
                         }
                     }

                 }
                 completion:^(BOOL finished){self.view.layer.shouldRasterize = NO;}];