2016-06-23 114 views
1

我想在UITableViewCell中画一个圆圈。在UIView框架内绘制圆圈

为了使定位更容易,我决定使用界面生成器放置UIView,设置约束,然后从该UIView内的代码中绘制圆。

这是它是如何应该看起来像: enter image description here

不过,我有一个问题,画圆就在广场取景当中。我想出了这个解决方案,但对我来说,它看起来更像是解决方法。内部

的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基本上是被认为是用于绘制圆的参考点的UIView。

我的问题是:

为什么不只是使用center.x和center.y,我为什么要中心。减去和起源的值来获得中点坐标?

+2

东西,如果你只是想要一个圈子查看有没有你'cornerRadious'用'UIView'尝试 –

+0

是的,这是另一种方法,我的问题更多的是好奇心,为什么我必须减少中心和原点才能真正获得中心? – DCDC

+0

@DCDC,这是因为当代码正在执行时,在运行时,视图在单元格的原点(超级视图)被初始化。只有在编译之后,它们才会在指定的坐标系中绘制。看到我的答案更清洁的方法。或者你必须在arccenter中对pageBadgeView中心的值进行硬编码,这将起作用。 –

回答

0

画圆圈UIView。查看设置Corner radius of layer的属性。为图层设置color

相应地快速修改您的代码。

let yCoord = (cellheight - circleview height)/2 
1

尝试是这样的

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 

} 
-1

尝试像,让你的看法圈

_circleView.layer.cornerRadius = _circleView.frame.size.width/2; 
_circleView.layer.borderColor = [UIColor redColor].CGColor; 
_circleView.layer.borderWidth = 2; 
_circleView.clipsToBounds = YES; 
+0

这是什么问题? – Anny

+0

我猜这是因为代码没有被注释掉,而且变量的名称与问题中的不相关。如果你在代码中实例化它们可能会很好,但是现在它看起来像复制粘贴而不是用户问题的详细答案。 –