【问题标题】:Mask over the image view Swift 5遮罩图像视图 Swift 5
【发布时间】:2021-08-28 10:28:45
【问题描述】:

我一直在尝试使用 Swift 5.2 从 this question 实现 this answer 我只看到模糊的图像并且没有出现蒙版。有人可以指出我在那里缺少什么或如何将其从 Swift 3 转换为 Swift 5 吗?

这是一个游乐场代码。

import UIKit
import PlaygroundSupport

let generalFrame = CGRect(x: 0, y: 0, width: 500, height: 500)
let containerView = UIView(frame: generalFrame)
containerView.backgroundColor = UIColor.black;
PlaygroundPage.current.liveView = containerView

let parentView = UIView(frame: generalFrame)
containerView.addSubview(parentView)
let url = URL(string: "https://static.pexels.com/photos/168066/pexels-photo-168066-large.jpeg")
let data = try Data(contentsOf: url!);
let imageView = UIImageView(frame:parentView.bounds)
imageView.image = UIImage(data: data)
imageView.contentMode = .scaleAspectFill

let maskView = UIView(frame:parentView.bounds)
maskView.backgroundColor = UIColor.black
maskView.layer.mask = {() -> CALayer in
    var  roundedRect = CGRect (
        x: 0.0,
        y: 0.0,
        width: parentView.bounds.size.width * 0.5,
        height: parentView.bounds.size.width * 0.5
    );
    roundedRect.origin.x = parentView.frame.size.width / 2 - roundedRect.size.width / 2;
    roundedRect.origin.y = parentView.frame.size.height / 2 - roundedRect.size.height / 2;
    let cornerRadius = roundedRect.size.height / 2.0;
    let path = UIBezierPath(rect:parentView.bounds)
    let croppedPath = UIBezierPath(roundedRect: roundedRect, cornerRadius: cornerRadius)
    path.append(croppedPath)
    path.usesEvenOddFillRule = true
    let maskLayer = CAShapeLayer()
    maskLayer.path = path.cgPath;
    maskLayer.fillRule = CAShapeLayerFillRule.evenOdd
    return maskLayer
}()

let blurView = UIBlurEffect(style: .light)
let effectView = UIVisualEffectView(effect: blurView)

effectView.frame = generalFrame
effectView.mask = maskView

parentView.addSubview(imageView)
parentView.addSubview(effectView)

【问题讨论】:

    标签: ios swift3 swift5


    【解决方案1】:

    我设法让这个工作。修复方法是对 UIVisualEffectView 应用掩码:

    let maskLayer = CAShapeLayer()
    effectView.layer.mask = maskLayer
    
    
    import UIKit
    import PlaygroundSupport
    
    let generalFrame = CGRect(x: 0, y: 0, width: 500, height: 500)
    let containerView = UIView(frame: generalFrame)
    containerView.backgroundColor = UIColor.black;
    PlaygroundPage.current.liveView = containerView
    
    let parentView = UIView(frame: generalFrame)
    containerView.addSubview(parentView)
    
    let url = URL(string: "https://static.pexels.com/photos/168066/pexels-photo-168066-large.jpeg")
    let data = try Data(contentsOf: url!);
    let imageView = UIImageView(frame:parentView.bounds)
    
    imageView.image = UIImage(data: data)
    imageView.contentMode = .scaleAspectFill
    
    var roundedRect = CGRect (
        x: 0.0,
        y: 0.0,
        width: parentView.bounds.size.width * 0.5,
        height: parentView.bounds.size.width * 0.5
    );
    roundedRect.origin.x = parentView.frame.size.width / 2 - roundedRect.size.width / 2;
    roundedRect.origin.y = parentView.frame.size.height / 2 - roundedRect.size.height / 2;
    let cornerRadius = roundedRect.size.height / 2.0;
    let path = UIBezierPath(rect:parentView.bounds)
    let croppedPath = UIBezierPath(roundedRect: roundedRect, cornerRadius: cornerRadius)
    path.append(croppedPath)
    path.usesEvenOddFillRule = true
    
    let maskLayer = CAShapeLayer()
    maskLayer.path = path.cgPath;
    maskLayer.fillRule = CAShapeLayerFillRule.evenOdd
    
    let blurView = UIBlurEffect(style: .light)
    let effectView = UIVisualEffectView(effect: blurView)
    effectView.frame = generalFrame
    effectView.layer.mask = maskLayer
    
    parentView.addSubview(imageView)
    parentView.addSubview(effectView)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-23
      • 2013-01-11
      • 2018-05-29
      • 2016-06-09
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多