2012-06-13 116 views
0

我试图实现一种方法,绘制一个基于手势识别的行,但我无法获得UIBezierPath显示。我知道手势识别器正在运行,因为每次激活该方法时都会打印记录。同样让我困惑的是,我之前绘制的蓝线试图绘制贝塞尔路径,但贝塞尔路径却没有。即使我手动添加任意点没有被绘制,比如:UIBezierPath没有绘制

[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]; 
} 

感谢您的帮助!

+0

你为什么要画与CG和一些与NSBezierPath一些路径之前调用移动到mypath中呢? myPath定义在哪里?请包括它的定义,以便我们看看它在做什么。 – user1118321

+0

谢谢你的回应!对不起,遗漏了初始值设定项。在 - (id)initWithFrame:(CGRect)框架中,我初始化为: 'myPath = [[UIBezierPath alloc] init]; [myPath moveToPoint:CGPointMake(0,0)];' – JShaev

回答

0

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

就如同

CGContextMoveToPoint(ctx, 0, 150); 

,你需要添加行

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

感谢您的回复!对不起,遗漏了初始值设定项。在 - (id)initWithFrame:(CGRect)框架中,我初始化并调用moveToPoint,如下所示: 'myPath = [[UIBezierPath alloc] init]; [myPath moveToPoint:CGPointMake(0,0)];' – JShaev

+0

@JShaev我测试了代码,它通过任意点绘制一条线就好了。你确定initWithFrame:被调用吗?在drawRect:中放置一个断点并在myPath上进行断言。 – MadhavanRP