简体   繁体   中英

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, ie 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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