【问题标题】:Circle/Rounded UIView Swift - not Square without corner radius圆形/圆形 UIView Swift - 不是没有角半径的方形
【发布时间】:2016-02-20 15:23:52
【问题描述】:

我想要一个“真正的”圆形 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")
    }
}

谢谢!!

【问题讨论】:

  • 你试过用谷歌搜索 uikit 重力圈吗? ;)

标签: ios swift uiview swift2


【解决方案1】:

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

所以它与绘图无关:它与您的视图如何碰撞有关,即动态动画系统认为它们的边缘在哪里。这个问题在 iOS 9 中通过将视图的collisionBoundsType 设置为圆形来解决。

【讨论】:

  • override var collisionBoundsType: UIDynamicItemCollisionBoundsType { return .Ellipse }修复了我的circleView中的问题,谢谢!
【解决方案2】:

在您的 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()
    }
}

希望我理解正确,它对你有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2014-01-20
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    相关资源
    最近更新 更多