如何创建椭圆形UIView?

时间:2015-12-30 06:35:59

标签: ios swift uiview

要制作圆圈视图,必须执行

shapeView.layer.cornerRadius = CGFloat(width)/2.0

所以我将这个逻辑扩展为椭圆形视图

if width >= height {
    shapeView.layer.cornerRadius = CGFloat(width)/2.0
} else{
    shapeView.layer.cornerRadius = CGFloat(height)/2.0
}

除了我的椭圆形视图有尖端,你可以看到here.

我的问题是:如何摆脱尖端并使它们成为圆形?

如果我有任何疑虑,请使用:

  • Swift 2
  • IOS 9.2
  • Xcode 7.2

2 个答案:

答案 0 :(得分:7)

使用var templates = template.New("template").Funcs(template.FuncMap{ "authenticated": func() bool { log.Println("Was I called?") return false }, "username": func() string { return "" }, }) 作为图层蒙版:

CAShapeLayer

演示:

class OvalView: UIView {

    override func layoutSubviews() {
        super.layoutSubviews()
        layoutOvalMask()
    }

    private func layoutOvalMask() {
        let mask = self.shapeMaskLayer()
        let bounds = self.bounds
        if mask.frame != bounds {
            mask.frame = bounds
            mask.path = CGPathCreateWithEllipseInRect(bounds, nil)
        }
    }

    private func shapeMaskLayer() -> CAShapeLayer {
        if let layer = self.layer.mask as? CAShapeLayer {
            return layer
        }
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.blackColor().CGColor
        self.layer.mask = layer
        return layer
    }

}

结果:

oval mask

答案 1 :(得分:0)

float w = 150.0f;
float h = 50.0f;
self.MyView.frame = CGRectMake(95, 461, w, w);
self.MyView.layer.cornerRadius = w / 2;

enter image description here