【问题标题】:Add blur mask on top and bottom of the screen in Objective-C在 Objective-C 屏幕的顶部和底部添加模糊蒙版
【发布时间】:2016-06-23 16:55:45
【问题描述】:

我需要像这样在屏幕的顶部和底部添加遮罩。

我怎样才能做到这一点?

【问题讨论】:

标签: ios objective-c uiimage gradient mask


【解决方案1】:
if (!UIAccessibilityIsReduceTransparencyEnabled()) {        
        self.BlurView.backgroundColor = [UIColor clearColor];
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        blurEffectView.frame = self.view.bounds;
        [blurEffectView setAlpha:1.0];
        [self.BlurView addSubview:blurEffectView];        
    }

其中self.BlurViewUIView 的类型。

您可以将其保留为您的上视图和下视图。并且可以根据您的上下尺寸调整框架blurEffectView.frame

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:
    1. 使用您的背景图片设置 UIImageView。
    2. 在顶部添加 UIVisualEffectView:

      UIVisualEffectView * vs = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
      vs.frame = self.view.bounds;
      [self.view addSubview:vs];
      
    3. 在顶部添加一个 UIView,然后将您的 UITableView(或其他)放入其中。
    4. 添加此代码:

      CAGradientLayer * layer = [CAGradientLayer layer];
      layer.frame = tableHolder.bounds;
      layer.colors = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
      layer.locations = @[@0.90,@0.99,@1.0];
      layer.startPoint = CGPointMake(0.0, 1.0f);
      layer.endPoint = CGPointMake(0.0f, 0.0f);
      tableHolder.layer.mask = layer;
      tableHolder.layer.masksToBounds = true;
      [tableHolder.layer insertSublayer:layer atIndex:0];
      

    根据您自己的需要调整变量,您必须更改位置。

    您不是“模糊”顶部和底部,而是通过渐变层逐渐掩盖它们。

    【讨论】:

      【解决方案3】:

      我可以想办法达到这个效果。但是您需要的不是模糊蒙版,您需要使用剪贴蒙版。

      1- 呈现一个模态视图控制器,其视图包含一个 UIVisualEffectView 在后面,一个滚动视图/tableview/collection 视图在前面,从顶部和底部偏移 40 像素(或者你想要的多少)。 2- 使用为任务量身定制的黑白图像向滚动视图添加蒙版,您可以使用以下代码添加蒙版:

      var maskImage = UIImage(name: "yourMask")
      var maskLayer = CALayer()
      maskLayer.frame = scrollView.bounds()
      maskLayer.contents = maskImage.CGImage
      scrollview.layer.mask = maskLayer
      

      3- 或者,您也可以使用 CAGradientLayer 来实现相同的效果(这也可以更好地用于缩放)。关于如何创建渐变层的堆栈溢出有很多解决方案,例如:Create gradient image programmatically in iOS

      话虽如此,我不明白为什么您的问题被否决了。我认为这是一个非常好的问题。

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-06
        • 2021-03-30
        • 2015-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-01
        相关资源
        最近更新 更多