Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c ShapeLayer HitTest在路径上对我无效_Objective C_Ios_Cocoa Touch - Fatal编程技术网

Objective c ShapeLayer HitTest在路径上对我无效

Objective c ShapeLayer HitTest在路径上对我无效,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我有一个简短的问题要问你们 我有一个用于设置框架的shapeLayer。 然后,我使用UIBezierPath在框架内容内绘制一个形状,我将其设置为shapeLayer.path 问题是,如何检测形状内部的点击为真,形状外部的点击为假 我一直在尝试各种疯狂的东西,但没有一个奏效 -(Boolean) isPoint:(CGPoint)point InsideShape:(CSAbstractShapeLayer*) shapeLayer fromSuperLayer:(CALayer*) sLa

我有一个简短的问题要问你们

我有一个用于设置框架的shapeLayer。 然后,我使用UIBezierPath在框架内容内绘制一个形状,我将其设置为shapeLayer.path

问题是,如何检测形状内部的点击为真,形状外部的点击为假

我一直在尝试各种疯狂的东西,但没有一个奏效

-(Boolean) isPoint:(CGPoint)point InsideShape:(CSAbstractShapeLayer*) shapeLayer fromSuperLayer:(CALayer*) sLayer
{ 
    CGPoint locationPoint   = point;
    locationPoint           = [sLayer convertPoint:locationPoint toLayer:shapeLayer];        
    CGAffineTransform at    = shapeLayer.affineTransform;            
    return CGPathContainsPoint([shapeLayer path],&at, locationPoint, NO);       
}


-(Boolean) isPoint:(CGPoint)point InsideShape:(CSAbstractShapeLayer*) shapeLayer fromSuperLayer:(CALayer*) sLayer
{ 
    CGPoint locationPoint   = point;        
    locationPoint           = [sLayer convertPoint:locationPoint toLayer:shapeLayer];        
    return [[UIBezierPath bezierPathWithCGPath:shapeLayer.path] containsPoint:locationPoint];    
}


-(Boolean) isPoint:(CGPoint)point InsideShape:(CSAbstractShapeLayer*) shapeLayer fromSuperLayer:(CALayer*) sLayer
{ 
    CGPoint locationPoint   = point;        
    locationPoint           = [sLayer convertPoint:locationPoint toLayer:shapeLayer];        
    CGRect theRect = CGPathGetBoundingBox(shapeLayer.path);
    return CGRectContainsPoint(theRect, locationPoint);    
}
注1:似乎我还不能发布一张图片,所以画一个内部有对角线的矩形,这个矩形 线的右侧是形状,所以我需要将“点击内部”检测为“是”,将“点击外部”(线的左侧和矩形的外部)检测为“否”

注2:这可能有帮助,但我无法理解……我发现,如果您创建shapeLayer并将CGPath添加到shapeLayer.path中,但不提供帧,则即使形状在屏幕上呈现,帧和shapeLayer的边界仍为[0,0,0,0],因此[view.layer hitTest:point]找不到作为目标的shapeLayer。因此,我为我的shapeLayer提供了一个框架和一个绑定


非常感谢您抽出时间

长话短说

在层级别,我使用从视图(locationInView)获得的点,然后对层进行转换,但该层有父层,因此我需要进行两次转换

如果你看看我前几天给你的方法

-(Boolean) isPoint:(CGPoint)point InsideShape:(CSAbstractShapeLayer*) shapeLayer fromSuperLayer:(CALayer*) sLayer
{

 
    CGPoint locationPoint   = point;

        
    locationPoint           = [sLayer convertPoint:locationPoint toLayer:shapeLayer];       

 
    return [[UIBezierPath bezierPathWithCGPath:shapeLayer.path] containsPoint:locationPoint];    

}
我必须做以下几件事

  • locationPoint=[sLayer.superlayer转换点:locationPoint到layer:sLayer]

  • locationPoint=[sLayer-convertPoint:locationPoint-to-layer:shapeLayer]

  • 所以从父层超层到它自己,然后从它到子层

    希望能帮助别人