【问题标题】:Draw a Line in UIView between 2 image views在 UIView 中在 2 个图像视图之间画一条线
【发布时间】:2012-07-23 07:39:57
【问题描述】:

我想在 UIView 中的图像视图(如链接图像..)之间画线。我用过

CGContextRef context    = UIGraphicsGetCurrentContext();

CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

CGContextSetLineWidth(context, 2.0);

CGContextMoveToPoint(context, 0,0); //start at this point

CGContextAddLineToPoint(context, 20, 20); //draw to this point

CGContextStrokePath(context);

但是在这里,我的上下文是空的。我不知道是否必须导入任何框架或导入任何标题。任何建议或样品将不胜感激。

提前致谢。

【问题讨论】:

  • 这段代码你在哪里写的?
  • 你必须导入 Quartz 框架并在头文件中写下这个:#import <QuartzCore/QuartzCore.h>,但是如果你没有导入它,那么你会得到编译器错误,所以这可能不是问题。
  • 我建议你阅读这篇文章stackoverflow.com/questions/5847876/…
  • 我在 ViewDidLoad 中写了这个

标签: iphone objective-c xcode


【解决方案1】:

最简单的方法是使用高度为 1 的标签。:)

UILabel *seperator=[[UILabel alloc]initWithFrame:CGRectMake(0, 53, 233, 1)];
seperator.backgroundColor=[UIColor redColor];

[loginView addSubview:seperator];

【讨论】:

  • 感谢您的支持,伙计。这是我的最终代码。我将 UIView 和 drawRect 方法子类化如下。CGContextRef context = UIGraphicsGetCurrentContext(); // 使用白色描边颜色绘制线条 CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); // 用 2.0 的笔画宽度绘制它们,这样它们就更明显了。 CGContextSetLineWidth(上下文, 5.0); // 从左到右画一条线 CGContextMoveToPoint(context, 90, 90.0); CGContextAddLineToPoint(context, 120.0, 150.0); CGContextStrokePath(context);
  • @user1402675:这取决于你。感谢您的评论。
【解决方案2】:
UIGraphicsBeginImageContext(image_signature.image.size);
[yourImageView.image drawInRect:CGRectMake(0, 0, yourImageView.image.size.width, yourImageView.image.size.height)]; 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0, 0);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 20, 20);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[yourImageView setImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();

...而“yourImageView”是一个图像视图,您将在其中画线。我建议你在每个上面的imageview上画线,在最底部,这是一个选项。

【讨论】:

  • 对不起朋友.. 没有什么对我有用.. 我已经在 ViewDidload 中编写了这段代码.. 我所需要的只是在两个图像视图之间画一条简单的线.. 我无法发布图片..
  • 老兄,该代码的作用就像一个魅力:将您的一个 imageViews 用作“yourImageView”,并在您在屏幕上显示 yourImageView 后立即执行该代码。
【解决方案3】:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
 CGContextSetLineWidth(context, 5.0); 
CGContextMoveToPoint(context, 90, 90.0); 
CGContextAddLineToPoint(context, 120.0, 150.0);
 CGContextStrokePath(context); 

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    相关资源
    最近更新 更多