2017-12-01 11 views
-5

나는 사지 또는 사변형과 같은 이미지를 만들고 싶습니다 enter image description here. 사다리꼴에 이미지를 놓습니다.Trapezium과 같은 Imageview를 만드는 방법

let path = UIBezierPath() 
     path.move(to: CGPoint(x: 0, y: 0)) 
     path.addLine(to: CGPoint(x: view.bounds.width, y: 0)) 
     path.addLine(to: CGPoint(x: self.view.bounds.width, y: self.view.bounds.height/2 - 60)) // 50 
     path.addLine(to: CGPoint(x: 0, y: view.bounds.size.width - 85)) // 80 
     path.close() 

     let shapeLayer = CAShapeLayer() 
     shapeLayer.path = path.cgPath 
     shapeLayer.fillColor = UIColor.black.cgColor 
     shapeLayer.strokeColor = UIColor.black.cgColor 
     view.layer.mask = shapeLayer 
+2

당신이 시도한 것을 보여주십시오. 우리는 당신을 위해 코드를 작성하지 않습니다. – Retterdesdialogs

+1

마스크 레이어와 UIBezierPath를 사용하십시오. (이처럼 간단한 폼에서는 꽤 쉬워야합니다). – Larme

+0

한번 시도하지 않고도이를 수행 할 수는 없으므로 도움을 청하십시오. –

답변

0
func addDiamondMask(to view: UIView) 
     { 

     let path = UIBezierPath() 
     path.move(to: CGPoint(x: 0, y: 0)) 
     path.addLine(to: CGPoint(x: view.bounds.width, y: 0)) 
     path.addLine(to: CGPoint(x: self.view.bounds.width, y: self.view.bounds.height/2 - 60)) 
     path.addLine(to: CGPoint(x: 0, y: view.bounds.size.width - 85)) 
     path.close() 

     let shapeLayer = CAShapeLayer() 
     shapeLayer.path = path.cgPath 
     shapeLayer.fillColor = UIColor.white.cgColor 
     shapeLayer.strokeColor = UIColor.gray.cgColor 
     view.layer.mask = shapeLayer 
    } 

은 사용 : addDiamondMask(to: imageView)

+0

작동하지 않습니다 ... –

+0

@ VaringSinghal. UR 이미지의 높이는 얼마입니까? –

0

은 당신의 이미지 뷰 함께 IBOutlet은 imgView로 이름 가정하자. 그런 다음이 코드를 사용하여 이미지보기의 모든 모양을 만들 수 있습니다.

func createShape() { 
    path = UIBezierPath() 
    path.move(to: CGPoint(x: self.imgView.frame.width/2, y: 0.0)) 
    path.addLine(to: CGPoint(x: 0.0, y: self.imgView.frame.size.height)) 
    path.addLine(to: CGPoint(x: self.imgView.frame.size.width, y: self.imgView.frame.size.height)) 
    path.close() 
    let mask = CAShapeLayer(); 
    mask.frame = imgView.bounds; 
    mask.path = path.cgPath; 
    imgView.layer.mask = mask; 
} 

이 예제는 삼각형 이미지 뷰를 생성합니다.