从UIViewController绘制一条线
问题描述:
我有一个UIViewController,它有一个UIImageView。我想在UIImageView上画一条线。我知道为了做到这一点,我必须重写UIView中的drawRect。有没有其他的选择?从UIViewController绘制一条线
答
重写drawRect可能是最好的解决方案,但总是有其他选择。
如果行是水平或垂直的,您可以使用所需行的框架创建UIView并设置其背景色。
您可以覆盖该行的图像,根据需要旋转/调整大小。
答
事情是这样的:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 1.0f);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, view.frame.size.width, view.frame.size.height);
CGContextStrokePath(context);
我下面回答,但你为什么不希望覆盖的drawRect? – picciano 2012-02-22 20:40:58
,因为如果只有一个带有drawRect的UIImage类就会很烦人? – adit 2012-02-22 22:07:32