2017-01-12 57 views
-3

我想在Swift 3中创建这种类型的视图。我们如何创建我无法做到这一点请帮助任何帮助,将不胜感激。在此先感谢Swift中的三角形Imageview

+1

**我想在Swift 3 **中创建这种类型的视图**它意味着什么? – seggy

+0

这听起来像你想用三角形路径掩盖图像视图? –

+0

对透明度有影响的三角形图像有帮助吗? – werediver

回答

1

是否要将图像裁剪为三角形?我为这种情况做了一个例子

func createTriangleImage() { 
    // your UIImage 
    let ballImage = UIImage(named: "ball")! 

    // your imageView which you wanted to be triangle. 
    // I made the example via layers as it seems easier to me 
    // so you dont need UIImageView here, UIView is enough 
    let viewWithImage = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) 

    // Layer for your image 
    let imageLayer = CALayer() 
    // Setting the image 
    imageLayer.contents = ballImage.cgImage 
    imageLayer.frame = viewWithImage.frame 
    viewWithImage.layer.addSublayer(imageLayer) 

    //view - is your main view where you wanted to put the triangle image 
    view.addSubview(viewWithImage) 

    // Creating a triangle path to draw triangle mask later 
    let path = UIBezierPath() 
    path.move(to: CGPoint(x: 20, y: 0)) 
    path.addLine(to: CGPoint(x: 40, y: 40)) 
    path.addLine(to: CGPoint(x: 0, y: 40)) 
    path.close() 

    // Creating a mask layer 
    let maskLayer = CAShapeLayer() 
    maskLayer.path = path.cgPath 
    maskLayer.fillColor = UIColor.red.cgColor 

    // Setting the mask 
    imageLayer.mask = maskLayer 
}