2

这个:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, 1)];
view.backgroundColor = [UIColor whiteColor];
view.alpha = 0.1;

或者这个:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, 1)];
view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.1];

还是有第三种选择?使用带有图像的 UIImageView?使用 CoreGraphics 进行自定义绘图?什么应该是最快的?

4

1 回答 1

1

最快的方法是创建一个 CALayer。如果需要,使用它可以让您轻松更改其颜色/不透明度。

CALayer *line = [CALayer new];
line.frame = CGRectMake(x, y, width, 1.0);
line.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1].CGColor;
[someView.layer addSublayer:line];

如果你想在现有视图上画一条线并让它保持不变,你可以将以下代码添加到现有视图的 drawRect: 方法中:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] colorWithAlphaComponent:0.1].CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextMoveToPoint(context, x, y);
CGContextAddLineToPoint(context, x + width, y);
CGContextStrokePath(context);
于 2012-12-04T22:54:30.660 回答