当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Circle/Rounded UIView Swift - not Square without corner radius

问题描述:

I would like to have a "real" rounded UIView, to avoid to have a square view with corner radius applied.

Because I'm using gravity, and my circles are not well displayed because they cannot touch each other in the corner (the screen is from the "Debug View Hierarchy" button to see the screen in 3D :

My actual code for the rounded 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")

}

}

Thank you !!

网友答案:

I'm using gravity, and my circles are not well displayed because they cannot touch each other in the corner

So it has nothing to do with drawing: it has to do with how your views collide, i.e. where the dynamic animation system thinks their edges are. This problem is solved in iOS 9 by setting the view's collisionBoundsType to a circle.

网友答案:

In your CircleView class, you can try use UIBezierPath(ovalInRect: rect) to get a real circle:

@IBDesignable class CircleView:UIView  {

override func drawRect(rect: CGRect) {

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

Hope I understood correctly and it helped you :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: