swift 具有低 alpha 的 ImageView 和透明显示父视图但具有 1 alpha 的 subImageView

swift ImageView with low alpha and subImageView transparent showing parent view but with 1 alpha

我无法设置我的 subImageViews 来显示其区域中 alpha 1 的父 ImageView,但我需要有 alpha 0.25。你有什么想法? 我试图通过 View 来完成,现在是 ImageView 但找不到解决方案。 中间的 16 个方块需要显示清晰的图像,按下按钮 "Start" 后它们会裁剪它们的区域并发送给其他 VC。 感谢帮助

所以我一直在尝试并找到了解决方案(也感谢@iOSDev link 的文章)。

如果有人需要类似的东西,就在这里:

@IBOutlet weak var alphaView: UIView!
@IBOutlet weak var pickedImage: UIImageView!
@IBOutlet weak var bigStackView: UIStackView!


override func viewWillAppear(_ animated: Bool) {
    pickedImage.image = imagePicked


    // Set white background color with custom alpha
    alphaView.backgroundColor = UIColor(white: 255/255, alpha: 0.85)

    // Create the initial layer from the stackView bounds.
    let maskLayer = CAShapeLayer()
    maskLayer.frame = alphaView.bounds

    // Create the frame to cover whole stackView
    let rect = CGRect(
        x: bigStackView.frame.minX,
        y: bigStackView.frame.minY,
        width: bigStackView.bounds.width,
        height: bigStackView.bounds.height)

    // Create the path
    let path = UIBezierPath(rect: alphaView.bounds)
    maskLayer.fillRule = CAShapeLayerFillRule.evenOdd

    // Append the framer to the path so that it is subtracted
    path.append(UIBezierPath(rect: rect))
    maskLayer.path = path.cgPath

    // Set the mask of the alphaview
    alphaView.layer.mask = maskLayer
}

pickedImage 是被 aplhaView 覆盖的背景图像视图,仅用于使其模糊,bigStackView 是中间的 16 个正方形,用于确定所需正方形的位置。也可以使用明确的 CGPoint 和 CGSize 值。

Screenshot from simulator