簡體   English   中英

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

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

我需要像這樣在屏幕的頂部和底部添加遮罩。

我怎樣才能做到這一點? 在此輸入圖像描述

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

希望這會幫助你。

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

根據自己的需要調整變量,您必須更改位置。

你不是“模糊”頂部和底部,而是通過漸變層逐漸掩蓋它們。

我能想出一種實現這種效果的方法。 但你需要的不是模糊面具,你需要使用剪貼蒙版。

1-提供一個模態視圖控制器,其視圖在后面包含一個UIVisualEffectView,前面的scrollview / tableview / collection視圖從頂部和底部偏移40px(或者你想要的多少)。 2-使用為任務定制的黑白圖像向滾動視圖添加遮罩,您可以使用以下代碼添加遮罩:

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

3-或者,您也可以使用CAGradientLayer來實現相同的效果(這也適用於縮放)。 關於如何創建漸變層的堆棧溢出有很多解決方案,如下所示: 在iOS中以編程方式創建漸變圖像

話雖如此,我不明白為什么你的問題被投了票。 我認為這是一個非常好的問題。

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM