2016-10-25 9 views
-1

矩形の比率を動的に変更したいが、半円の比率は変更したい。半円は、下に整列し、水平方向の中央する必要があります。UIImageでより多くのスライスラインを使用できますか?

enter image description here

がどのように私はXcodeでUIImageに多くのスライスラインを追加することができますか?

アンドロイド用のNine Patchエディタで作成しましたが、iOSと互換性がありません。

enter image description here

+0

またはどのようにプログラム的にそれを実装するには? –

+0

plsはあなたがアンドロイドと関係ない限り、アンドロイドタグで質問にタグを付けません。 –

+0

あなたが望むように画像を伸ばす方法はありませんので、CAShapeLayerを使ってプログラムで行う必要があります。迅速かobj-cを選んでいますか? – alexburtnik

答えて

1

は、あなたが望むようにUIImageを伸ばす方法はありませんが、あなたはプログラムでそれを行うことができます。私はCAShapeLayer内部でカスタムUIViewのを実装しました:

import UIKit 

class CustomShapeView: UIView { 

    private let shapeLayer = CAShapeLayer() 
    var radius = CGFloat(0) 
    var shadowRadius = CGFloat(5) 

    override func draw(_ rect: CGRect) { 
     backgroundColor = UIColor.clear 

     shapeLayer.frame = self.bounds 
     shapeLayer.fillColor = UIColor.white.cgColor 
     shapeLayer.path = createShapePath().cgPath 

     shapeLayer.shadowPath = shapeLayer.path 
     shapeLayer.shadowColor = UIColor.black.cgColor 
     shapeLayer.shadowOffset = .zero 
     shapeLayer.shadowRadius = shadowRadius 
     shapeLayer.shadowOpacity = 0.5 

     layer.addSublayer(shapeLayer) 
    } 

    func createShapePath() -> UIBezierPath { 
     let path = UIBezierPath() 

     let w = self.bounds.size.width 
     let h = self.bounds.size.height 
     let circleLever: CGFloat = radius * 0.552 

     path.move(to: CGPoint(x: w/2 - radius, y: h)) 
     path.addLine(to: CGPoint(x: 0, y: h)) 
     path.addLine(to: CGPoint(x: 0, y: 0)) 
     path.addLine(to: CGPoint(x: w, y: 0)) 
     path.addLine(to: CGPoint(x: w, y: h)) 
     path.addLine(to: CGPoint(x: w/2 + radius, y: h)) 

     path.addCurve(to: CGPoint(x: w/2, y: h - radius), controlPoint1:CGPoint(x: w/2 + radius, y: h - circleLever), controlPoint2:CGPoint(x: w/2 + circleLever, y: h - radius)) 
     path.addCurve(to: CGPoint(x: w/2 - radius, y: h), controlPoint1:CGPoint(x: w/2 - circleLever, y: h - radius), controlPoint2:CGPoint(x: w/2 - radius, y: h - circleLever)) 
     path.close() 
     path.move(to: CGPoint(x: w/2 - radius, y: h)) 

     return path 
    }   
} 

あなたは、円の中心は常にビューの下端になりますどんなサイズとしたい任意の半径を設定することができます。また、shadowRadiusを変更することもできます。 Interface Builderで

セットアップ:

enter image description here

結果:

enter image description here

関連する問題