Possible Duplicate:
How can I check if a user tapped near a CGPath?檢測上UIBezierPath中風觸摸,不填
我下面從這裏
蘋果的指導,試圖只在我的UIBezierPath的撫摸部分檢測的觸摸事件。如果我使用UIBezierPath方法containsPoint,它可以正常工作,但它檢測筆觸和UIBezierPath的填充部分上的觸摸事件,並且我希望它只出現在筆觸部分上。
繼蘋果指南(在鏈路的上市3-6),我創造了這個功能:
- (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;
}
當我打電話吧,我得到:
錯誤:CGContextSaveGState:無效的上下文爲0x0
錯誤:CGContextAddPath:無效的情況下爲0x0
錯誤:CGContextPathContainsPoint:無效的情況下爲0x0
錯誤:CGContextRestoreGState:無效的情況下爲0x0
這是我的觀點的drawRect功能,我的代碼來創建我的路徑:
- (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];
}
然後我試圖調用containsPoint功能touchesMoved內,像這樣:
if (![self containsPoint:c onPath:[self createPath] inFillArea:NO]) return;
您是否曾經爲此找到過解決方案?這篇文章是互聯網上排名第一的所有與此有關的問題,但沒有答案:) – shawnwall 2012-01-23 18:46:58
我把我的聲音添加到shawnall's,你是否設法解決了這個問題?希望你可以分享這個解決方案,如果你有任何 – Abolfoooud 2012-08-28 09:56:54