Drawing circle inside UIView frame

I'd like to draw a circle inside UITableViewCell.

To make it easier with positioning, I decided to place UIView using interface builder, set constraints, and then draw the circle from code inside this UIView.

This is how it's supposed to look like: 在这里输入图像描述

However, I had a problem with drawing the circle right in the middle of the square view. I figured out this solution, but to me it looks more like workaround. Inside

tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

let xCoord = cell.pageBadgeView.center.x - cell.pageBadgeView.frame.origin.x
let yCoord = cell.pageBadgeView.center.y - cell.pageBadgeView.frame.origin.y

let circlePath = UIBezierPath(arcCenter: CGPoint(x: xCoord ,y: yCoord), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

pageBadgeView is basically the UIView that is supposed to be the reference point for the drawing circle.

My question is:

Why don't just use center.x and center.y, why did I have to substract values of center and origin to get the middle coordinate?


尝试这样的事情

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {

var someName = UIView()
var someNameRadius = 25.0
let cell = tableView.dequeueReusableCellWithIdentifier("experienceCell", forIndexPath: indexPath) as! YourCellClass

someName.backgroundColor = UIColor.clearColor()
someName.layer.borderWidth = 3.0
someName.layer.borderColor = UIColor.redColor().CGColor
someName.center = cell.contentView.center
someName.frame.size = CGSize(width: someNameRadius*2, height: someNameRadius*2)
someName.layer.cornerRadius = someNameRadius  // Note, this is half of the width of the view's width
cell.contentView.addSubView(someName)


return cell

}

To draw circle take UIView . Set Corner radius of layer property of View. Set color for layer.

Modify your code in swift accordingly.

 let yCoord = (cellheight - circleview height)/2

试试让你的视图变成圆形

_circleView.layer.cornerRadius = _circleView.frame.size.width/2;
_circleView.layer.borderColor = [UIColor redColor].CGColor;
_circleView.layer.borderWidth = 2;
_circleView.clipsToBounds = YES;
链接地址: http://www.djcxy.com/p/60510.html

上一篇: 在C ++中初始化静态std :: map <int,int>

下一篇: 在UIView框架内绘制圆圈