2017-01-12 79 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 
}