【问题标题】:How do i mask UIImage view inside UIView?如何在 UIView 中屏蔽 UIImage 视图?
【发布时间】:2017-05-09 01:11:42
【问题描述】:

我试图将子视图屏蔽为超级视图,但图像视图未屏蔽为超级视图

显示方式

应该如何

代码

class TicketView: UIView {

    var img = UIImage(named: "social-event.jpg")
    var imageView = UIImageView()

    override func draw(_ rect: CGRect) {
        let path = UIBezierPath(rect: rect)
        let width:CGFloat = 11
        let height:CGFloat = 11
        let roundLeft = UIBezierPath(roundedRect: CGRect(x: -(width/2), y: -(height/2), width: width, height: height), cornerRadius: (height/2))
        let roundLeftBotton = UIBezierPath(roundedRect: CGRect(x: -(width/2), y: (rect.height)-(height/2), width: width, height: height), cornerRadius: (height/2))
        let roundRight = UIBezierPath(roundedRect: CGRect(x: (rect.width)-(width/2), y: -(height/2), width: width, height: height), cornerRadius: (height/2))
        let roundRightBottom = UIBezierPath(roundedRect: CGRect(x: (rect.width)-(width/2), y: (rect.height)-(height/2), width: width, height: height), cornerRadius: (height/2))

        let roundRightMiddle = UIBezierPath(roundedRect: CGRect(x: (rect.width*0.7)-(width/2), y: -(height/2), width: width, height: height), cornerRadius: (height/2))
        let roundRightMiddleBottom = UIBezierPath(roundedRect: CGRect(x: (rect.width*0.7)-(width/2), y:
            (rect.height)-(height/2), width: width, height: height), cornerRadius: (height/2))
        path.append(roundLeft.reversing())
        path.append(roundLeftBotton.reversing())
        path.append(roundRight.reversing())
        path.append(roundRightBottom.reversing())
        path.append(roundRightMiddle.reversing())
        path.append(roundRightMiddleBottom.reversing())

        UIColor.green.setFill()
        path.fill()

        imageView.frame = CGRect(x: 0, y: 0, width: (rect.width * 0.7),  height: rect.height)
        imageView.backgroundColor = UIColor.black
        self.addSubview(imageView)


    }

}

我到现在为止的尝试

1) 遮罩层

2) 剪切图像

【问题讨论】:

  • 您是否尝试在添加为子视图之前向图像视图添加蒙版?
  • @iOS_devloper 你的意思是创建一个 CAShapeLayer 并屏蔽它吗?
  • 是的。创建 CAShapeLayer 并在 Imageview 上对其进行遮罩。
  • @iOS_devloper 谢谢 :)

标签: ios swift uiview uiimage core-graphics


【解决方案1】:

制作具有像你的角落一样的图层的新图像,并将你的视图作为蒙版。

【讨论】:

    猜你喜欢
    • 2011-03-04
    • 2013-08-20
    • 2011-02-16
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2011-10-11
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多