LoginSignup
26
26

More than 5 years have passed since last update.

UIBezierPathで角丸にする

Last updated at Posted at 2014-11-01

角丸をまとめてみた。

UIBezierPathのclipを使って角丸にする

UIViewを継承したClipRoundedCornersViewというのを作ってイメージを描画しました。

import UIKit

class RoundedCornersView: UIView {

    override func drawRect(rect: CGRect) {
        UIBezierPath(roundedRect: self.bounds, cornerRadius: CGRectGetHeight(self.bounds) / 2).addClip()
        UIImage(named: "rocket")?.drawInRect(self.bounds)
    }
}

UIBezierPathとUIViewのlayerを使って角丸にする

こちらもUIViewをサブクラス化して作りました。

import UIKit

class LayerRoundedCornersView: UIView {

    override func drawRect(rect: CGRect) {
        UIImage(named: "rocket")?.drawInRect(self.bounds)

        let maskPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: CGRectGetWidth(self.bounds) / 2)
        let maskLayer = CAShapeLayer()
        maskLayer.path = maskPath.CGPath
        self.layer.mask = maskLayer;
    }
}

UIStoryboard

今回は、UIStoryboardに置いたUIViewのクラスを指定することで画面上に表現しました。

storyboard.png

実行結果

app2.png

サンプルコード

参考

26
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
26