繁体   English   中英

圆形/圆形UIView Swift-没有角半径的不是正方形

[英]Circle/Rounded UIView Swift - not Square without corner radius

我想拥有一个“真实的”圆形UIView,以避免使用带有拐角半径的正方形视图。

因为我使用的是重力,所以我的圆不能很好地显示,因为它们无法在角落相互接触(屏幕来自“ Debug View Hierarchy”按钮,以3D形式显示屏幕:

在此处输入图片说明

我的四舍五入的UIView的实际代码:

 import Foundation
import UIKit
@IBDesignable class CircleView:UIView  {
    override init (frame : CGRect) {
        super.init(frame : frame)
        self.layer.cornerRadius = frame.width / 2
        self.clipsToBounds = true
        addBehavior()
    }
    convenience init () {
        self.init(frame:CGRect(x: 0, y: 0, width: 50   , height: 50))
        self.layer.cornerRadius = self.frame.width / 2
        self.clipsToBounds = true
    }
    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }
    func addBehavior (){
        //print("Add all the behavior here")
    }
}

谢谢 !!

我正在使用重力,我的圈子显示得不好,因为它们在角落里无法互相接触

因此,它与绘图无关:它与视图的碰撞方式(即动态动画系统认为其边缘在何处)有关。 在iOS 9中,可以通过将视图的collisionBoundsType设置为圆形来解决此问题。

在您的CircleView类中,您可以尝试使用UIBezierPath(ovalInRect:rect)获得一个真实的圆:

@IBDesignable class CircleView:UIView  {

override func drawRect(rect: CGRect) {

        let circlePath = UIBezierPath(ovalInRect: rect)
        let color = UIColor.orangeColor()
        color.set()
        circlePath.fill()
    }
}

希望我能正确理解,对您有帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM