UIBezierpath对中风没有反应

时间:2013-07-21 08:12:51

标签: ios cocoa-touch uibezierpath quartz-core

我使用以下代码在UIBezierPath的{​​{1}}上绘制UIImageView。 但它没有显示出绿色的道路。

UIView

我需要在绘制- (void)drawRect:(CGRect)rect { [[UIColor blackColor] setStroke]; [aPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; self->aPath = [[UIBezierPath alloc]init]; CAShapeLayer* greenPath = [CAShapeLayer layer]; greenPath.path = aPath.CGPath; [greenPath setFillColor:[UIColor greenColor].CGColor]; [greenPath setStrokeColor:[UIColor blueColor].CGColor]; greenPath.frame=CGRectMake(0, 0,100,30); //add shape layer to view's layer [[imgView layer] addSublayer:greenPath]; aPath.lineCapStyle=kCGLineCapRound; aPath.miterLimit=0; aPath.lineWidth=10; [aPath moveToPoint:[mytouch locationInView:imgView]]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; [aPath addLineToPoint:[mytouch locationInView:imgView]]; [imgView setNeedsDisplay]; } 的任何地方显示绿线。

1 个答案:

答案 0 :(得分:1)

你的代码中有很多奇怪的东西,但是它运行了,问题在这里

[imgView setNeedsDisplay];

正如我在您的代码中看到的那样,您希望它能够在drawRect上运行,但drawRect只会在当前视图上的setNeedsDisplay上被调用,不在imgView上,将其切换为

[self setNeedsDisplay];

它会起作用。你可能需要继承UIImageView来处理它内部的drawRect。另外,我猜你想要实际修改图像,因此尝试在其中进行绘制,尝试阅读在图像上下文中使用CoreGraphics。

关于其他问题

我看到的主要问题是,你现在混淆了CALayers和CoreGraphics,因为它现在正在使用,你添加到imageView的CALayer完全没用了。只需使用drawRect并在移动时设置BezierPath即可。

另外,使用->understand what you're getting yourself into时要小心,我通常建议坚持@property,并使用self.myVar_myVar