gpt4 book ai didi

iphone - 在CAShapelayer中获取接触点

转载 作者:行者123 更新时间:2023-12-01 19:22:59 25 4
gpt4 key购买 nike

我正在通过以下代码绘制线条:

 - (void) drawLine:(float)x :(float)y:(float)toX:(float)toY 
{
CAShapeLayer *lineShape = nil;
CGMutablePathRef linePath = nil;
linePath = CGPathCreateMutable();
lineShape = [CAShapeLayer layer];

lineShape.lineWidth = 1.0f;
lineShape.lineCap = kCALineJoinMiter;
lineShape.strokeColor = [[UIColor redColor] CGColor];


CGPathMoveToPoint(linePath, NULL, x, y);
CGPathAddLineToPoint(linePath, NULL, toX, toY);

lineShape.path = linePath;
CGPathRelease(linePath);

if(x != 0 && y != 0)
[myView.layer addSublayer:lineShape];

}

现在,我想知道我的线路何时打动。这怎么可能 ?
我正在使用

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSIndexSet *indexSet = [myView.layer.sublayers indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
return [obj isMemberOfClass:[CAShapeLayer class]];
}];

NSArray *textLayers = [myView.layer.sublayers objectsAtIndexes:indexSet];
for (CAShapeLayer *textLayer in textLayers) {
CGPoint p = [[touches anyObject] locationInView:myView];
NSLog(@"touch x is :%f",p.x);

CGAffineTransform transf = CGAffineTransformMakeTranslation(-textLayer.position.x, - textLayer.position.y);

if(CGPathContainsPoint(textLayer.path, &transf, p, NO)){
NSLog(@"touched..");
}
}

}
但是通过 CGPathContainsPoint 方法,我没有得到该触摸是否属于我的行路径。

最佳答案

以下代码对我有用

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

CGPoint p = [[touches anyObject] locationInView:self.view];

if(CGPathContainsPoint(textLayer.path,nil, p, NO))
{

NSLog(@"touched");
// the touch is inside the shape
}

}

关于iphone - 在CAShapelayer中获取接触点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9224115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com