Ios 创建一个类似Apple Mail应用程序的圆形TableViewCell指示器
我正在尝试为我的TableViewCell创建一个循环指示器,类似于Apple Mail应用程序中标记为未读的电子邮件时的指示器 我有以下代码:Ios 创建一个类似Apple Mail应用程序的圆形TableViewCell指示器,ios,swift,Ios,Swift,我正在尝试为我的TableViewCell创建一个循环指示器,类似于Apple Mail应用程序中标记为未读的电子邮件时的指示器 我有以下代码: var Indicator = CAShapeLayer() var IndicatorSize: CGFloat = 10 // standard blue color let blueColor = UIColor(red: 0, green: 122.0/255.0, blue:1.0, alpha:1) // create circula
var Indicator = CAShapeLayer()
var IndicatorSize: CGFloat = 10
// standard blue color
let blueColor = UIColor(red: 0, green: 122.0/255.0, blue:1.0, alpha:1)
// create circular CAShapeLayer
Indicator.bounds = CGRect(x:0, y:0, width:IndicatorSize, height:IndicatorSize)
Indicator.position = CGPoint(x: 10, y: 10)
Indicator.cornerRadius = Indicator.frame.size.width/2
// add as cell sublayer
cell.layer.addSublayer(Indicator)
虽然这似乎可以做到这一点,但圆圈感觉不像邮件应用程序中的圆圈那么平滑,角落看起来几乎是像素化的。我不确定这是否是因为我正在使用CAShapeLayer。我想知道是否有更好的选择
我还可以将其作为单元的对象,而不仅仅是一个层,这样我就可以使用cell.Indicator调用它了?使用
UIView
。对drawRect:
方法进行子类化,并在其中绘制一个UIBezierPath
比如:
@implementation BlueCircleView
- (void)drawRect:(CGRect)rect
{
[[UIColor blueColor] setFill];
// Assuming your view as the size of the dot you want.
// Otherwise, simply pass another rect.
UIBezierPath *round = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
[round fill];
}
@end
@基思本特:这不是一个疑难解答问题。这个问题的标签是Swift。答案应该是Swift,而不是Objective-C。大多数情况下,
rect
就可以了。但我们必须意识到,需要在屏幕上重新绘制的是“唯一”矩形,而不一定与整个点矩形完全相同。无论如何,这个案子的细节。这是一个很好的观点。而且这个实现假设视图总是一个完美的正方形,圆点会占据整个视图,从而形成圆点。但这些代码足以让想要启动它的人启动。我正在使用UITableViewCellStyle.Subtitle。我是否必须切换到自定义单元格,创建UIView并将其关联到CircleView类,或者是否有其他方法来执行此操作?是的,您需要创建自定义表视图单元格并将视图添加到其中。是否有方法可以发送颜色以根据操作更新CircleView?我有以下行,但它似乎没有更新:cell.CircleIndicator.tintColor=UIColor.yellowColor()
class CircleView: UIView {
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
self.tintColor.setFill()
CGContextFillEllipseInRect(context, rect)
}
}