【发布时间】:2013-08-13 03:33:33
【问题描述】:
我已添加此方法来检查 CGPoint 是否在 UIBezierPath 内
- (BOOL)isPointInPath:(CGPoint)point inShape:(BOOL)inShape {
CGContextRef context = UIGraphicsGetCurrentContext();
CGPathRef cgPath = path.CGPath;
CGPathDrawingMode mode = kCGPathStroke;
if (inShape) mode = kCGPathFill;
CGContextSaveGState(context);
CGContextAddPath(context, cgPath);
bool isHit = CGContextPathContainsPoint(context, point, mode);
CGContextRestoreGState(context);
return isHit;
}
path 是在头文件中声明的UIBezierPath。
当我在 drawRect: 方法之外调用此方法时,我收到以下 4 条错误消息:
CGContextSaveGState: invalid context 0x0
CGContextAddPath: invalid context 0x0
CGContextPathContainsPoint: invalid context 0x0
CGContextRestoreGState: invalid context 0x0
但是当我在drawRect: 中调用它并且在绘图之后它可以工作。
为什么它在drawRect: 之外不起作用?这对我来说毫无意义。
【问题讨论】:
标签: ios boolean drawrect cgcontext uibezierpath