【问题标题】:UIBezierPath Not DrawingUIBezierPath 未绘制
【发布时间】:2012-06-13 21:42:46
【问题描述】:

我正在尝试实现一种基于手势识别绘制线条的方法,但我无法显示 UIBezierPath。我知道手势识别器正在运行,因为每次激活该方法时我都会打印以记录。让我感到困惑的是,我在尝试绘制 BezierPath 之前绘制的蓝线会显示,但 BezierPath 不会。即使我手动添加任意点也不会绘制任何内容,例如:

[myPath addLineToPoint:CGPointMake(50, 50)];

这是我的 UIView 中的代码:

- (void)drawRect:(CGRect)rect
{

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSetRGBStrokeColor(ctx, 0, 0, 1.0, 1); 
CGContextMoveToPoint(ctx, 0, 150);
CGContextAddLineToPoint( ctx, 480, 150);
CGContextStrokePath(ctx);

[myPath addLineToPoint:CGPointMake(50, 50)];
[myPath stroke];
}

- (IBAction)handlePan:(UIPanGestureRecognizer *) recognizers {

CGPoint translation = [recognizers translationInView:self];
NSLog(@"Logged");

[myPath addLineToPoint:CGPointMake(translation.x, translation.y)];
[self setNeedsDisplay];
}

感谢您的帮助!

【问题讨论】:

  • 你为什么用 CG 画一些路径,而用 NSBezierPath 画一些? myPath 在哪里定义?请包括它的定义,以便我们可以看到它在做什么。
  • 感谢您的回复!很抱歉省略了初始化程序。在 - (id)initWithFrame:(CGRect)frame 我这样初始化:myPath=[[UIBezierPath alloc]init]; [myPath moveToPoint:CGPointMake(0, 0)];

标签: ios uiview uigesturerecognizer drawrect uibezierpath


【解决方案1】:

你在哪里初始化 myPath?确保它已初始化。

就像

CGContextMoveToPoint(ctx, 0, 150);

,您需要在添加行之前调用 move 到 myPath 上的点

[myPath moveToPoint:CGPointMake(0,150)];
[myPath addLineToPoint:CGPointMake(50, 50)];

【讨论】:

  • 感谢您的回复!很抱歉省略了初始化程序。在 - (id)initWithFrame:(CGRect)frame 内,我初始化并调用 moveToPoint:myPath=[[UIBezierPath alloc]init]; [myPath moveToPoint:CGPointMake(0, 0)];
  • @JShaev 我测试了代码,它通过任意点画了一条线就好了。您确定调用了 initWithFrame: 吗?在 drawRect:. 中放置断点并在 myPath 上进行断言
猜你喜欢
  • 1970-01-01
  • 2012-06-09
  • 2011-09-20
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-05
相关资源
最近更新 更多