2017-12-01 16 views
-5

私は台形または四辺形のようなイメージを作りたいenter image description here。そして、TrapeziumのImageを設定します。私たちは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

@VarunSinghal。ウルイメージの高さは何ですか? –

0

あなたImageViewの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; 
} 

この例では、三角画像ビューが作成されます。

関連する問題