简体   繁体   English

在Objective-C中在屏幕的顶部和底部添加模糊遮罩

[英]Add blur mask on top and bottom of the screen in Objective-C

I need to add mask on top and buttom of the screen like this. 我需要像这样在屏幕的顶部和底部添加遮罩。

How can I achieve this ? 我怎样才能做到这一点? 在此输入图像描述

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];        
    }

Where self.BlurView is type of UIView . self.BlurViewUIView类型。

You can keep this for your upper and bottom view . 您可以保留上部和底部视图。 And can adjust frame blurEffectView.frame according to your upper and bottom dimensions . 并且可以根据您的上下尺寸调整框架blurEffectView.frame

Hope this will help you. 希望这会帮助你。

  1. Set up UIImageView with your background image. 使用您的背景图像设置UIImageView。
  2. Add UIVisualEffectView over the top: 在顶部添加UIVisualEffectView:

     UIVisualEffectView * vs = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]]; vs.frame = self.view.bounds; [self.view addSubview:vs]; 
  3. Add a UIView over the top, and put your UITableView (or whatever) in that. 在顶部添加一个UIView,并将UITableView(或其他)放入其中。
  4. Add this code: 添加此代码:

     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]; 

Adjust the variables for your own needs, you'll have to change the locations. 根据自己的需要调整变量,您必须更改位置。

You're not 'blurring' the tops and bottoms, but gradually masking them out via the gradient layer. 你不是“模糊”顶部和底部,而是通过渐变层逐渐掩盖它们。

I can think of a way to achieve this effect. 我能想出一种实现这种效果的方法。 But what you need is not a blur mask, you need to use a clipping mask. 但你需要的不是模糊面具,你需要使用剪贴蒙版。

1- present a modal view controller with a view that contains a UIVisualEffectView in the back, and a scrollview/tableview/collection view in the front that is offset 40px (or however much you want) from the top and bottom. 1-提供一个模态视图控制器,其视图在后面包含一个UIVisualEffectView,前面的scrollview / tableview / collection视图从顶部和底部偏移40px(或者你想要的多少)。 2- Add a mask to the scroll view using a black and white image tailored to the task, you can add the mask using the following code: 2-使用为任务定制的黑白图像向滚动视图添加遮罩,您可以使用以下代码添加遮罩:

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

3- Alternatively, you can also use CAGradientLayer to achieve the same effect (this would work better with scaling as well). 3-或者,您也可以使用CAGradientLayer来实现相同的效果(这也适用于缩放)。 There's plenty of solutions on stack overflow on how to create a gradient layer, like this one: Create gradient image programmatically in iOS 关于如何创建渐变层的堆栈溢出有很多解决方案,如下所示: 在iOS中以编程方式创建渐变图像

Having said that I don't understand why your question is down voted. 话虽如此,我不明白为什么你的问题被投了票。 I think it's a perfectly good question to make. 我认为这是一个非常好的问题。

Hope this helps! 希望这可以帮助!

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

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