检测UIBezierPath笔触上的触摸,而不是填充 [英] Detect touch on UIBezierPath stroke, not fill
问题描述
我从这里下载Apple的指南
I'm following Apple's guide from here
只尝试在我的UIBezierPath的描边部分检测触摸事件。如果我使用UIBezierPath方法,containsPoint,它工作正常,但它检测触摸事件的笔触和UIBezierPath的填充部分,我希望它只发生在抚摸部分。
to try and detect a touch event only on the stroked portion of my UIBezierPath. If I use the UIBezierPath method, containsPoint, it works fine, but it detects touch events on the stroke and the fill portion of the UIBezierPath, and I want it to only occur on the stroked portion.
按照苹果指南(链接列表3-6),我创建了此功能:
Following Apple's guide (at listing 3-6 of the link), I created this function:
- (BOOL)containsPoint:(CGPoint)point onPath:(UIBezierPath*)path inFillArea:(BOOL)inFill
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGPathRef cgPath = path.CGPath;
BOOL isHit = NO;
// Determine the drawing mode to use. Default to
// detecting hits on the stroked portion of the path.
CGPathDrawingMode mode = kCGPathStroke;
if (inFill)
{
// Look for hits in the fill area of the path instead.
if (path.usesEvenOddFillRule)
mode = kCGPathEOFill;
else
mode = kCGPathFill;
}
// Save the graphics state so that the path can be
// removed later.
CGContextSaveGState(context);
CGContextAddPath(context, cgPath);
// Do the hit detection.
isHit = CGContextPathContainsPoint(context, point, mode);
CGContextRestoreGState(context);
return isHit;
}
当我调用它时,我会得到:
When I call it, I get:
错误:CGContextSaveGState:无效上下文0x0
错误:CGContextAddPath:无效上下文0x0
错误:CGContextPathContainsPoint:无效上下文0x0
错误:CGContextRestoreGState:invalid context 0x0
Error: CGContextSaveGState: invalid context 0x0
Error: CGContextAddPath: invalid context 0x0
Error: CGContextPathContainsPoint: invalid context 0x0
Error: CGContextRestoreGState: invalid context 0x0
这是我的view的drawRect函数,我的代码创建我的路径:
Here's my view's drawRect function, and my code to create my path:
- (UIBezierPath *) createPath {
static UIBezierPath *path = nil;
if(!path) {
path = [[UIBezierPath bezierPathWithOvalInRect:CGRectMake(35, 45, 250, 250)] retain];
path.lineWidth = 50.0;
[path closePath];
}
return path;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
- (void)drawRect:(CGRect)rect
{
// Drawing code
//draw a circle
UIBezierPath *aPath = [self createPath];
//set stroke width
aPath.lineWidth = 50.0;
//set stroke color
[[UIColor blueColor] setStroke];
//stroke path
[aPath stroke];
//close path
[aPath closePath];
//add a label at the "top"
UILabel *topLabel;
topLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, -5, 80, 25)];
topLabel.text = @"The Top";
[self addSubview:topLabel];
}
然后我试图调用touchesMoved里面的containsPoint函数, :
I'm then trying to call the containsPoint function inside of touchesMoved, like this:
if (![self containsPoint:c onPath:[self createPath] inFillArea:NO]) return;
推荐答案
touchesMoved
方法),没有当前上下文,因此 UIGraphicsGetCurrentContext()
将返回NULL。
When you're currently not drawing (as in the touchesMoved
method), there is no current context, so UIGraphicsGetCurrentContext()
will return NULL.
由于你使用的上下文是使用它的当前路径,你可以使用 CGPathContainsPoint
CGContextPathContainsPoint
。
As all you're doing with the context is to use its current path, you could just as well use CGPathContainsPoint
instead of CGContextPathContainsPoint
.
这篇关于检测UIBezierPath笔触上的触摸,而不是填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!