ios - 遮盖 ImageView Swift 5

标签 ios swift3 swift5

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

这是 Playground 代码。

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)

最佳答案

我设法让它工作。解决方法是将 mask 应用于 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)

enter image description here

关于ios - 遮盖 ImageView Swift 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67937670/

相关文章:

ios - 如何从 HTML 应用程序为 IOS 设备创建唯一标识符

iOS静态库+预处理头文件

ios - 将 BindableObject 传递给 View

swift - 无法设置 AsyncStream `onTermination` 处理程序

ios - 'withUnsafeMutableBytes' 在 Swift 5 中被弃用

ios - 隐藏标签栏导航 Controller 中的后退按钮

arrays - Swift:检测字符串的首字母何时不等于特定字符?

iOS - 注销后重置 UITabBarController/UIViewController 状态

swift - 如果用户不允许某些条目类型,HealthKit 无法使用多个条目编写 HKSample

swift - 如何在 Swift 5 中正确使用子类