iOS:CAShapeLayer、CALayer掩码、hitTest:WithEvents、hitTest:
提前感谢:我有如下视图层次结构: AiOS:CAShapeLayer、CALayer掩码、hitTest:WithEvents、hitTest:,ios,calayer,hittest,cashapelayer,Ios,Calayer,Hittest,Cashapelayer,提前感谢:我有如下视图层次结构: Aui视图 在这个集合中:CALayer*layer=(CALayer*)self.layer 在这个类中,我有一个CAShapeLayer,它被设置为CALayer的掩码 它的动画很好,没问题 我有一个UIViewController,通过以下方式初始化上述UIView: myView=[myAnimationView alloc]initWithFrame:(CGRect){{{0,0},320,450}]; [self.view addSubview my
ui视图
在这个集合中:CALayer*layer=(CALayer*)self.layer
在这个类中,我有一个CAShapeLayer
,它被设置为CALayer
的掩码
它的动画很好,没问题
我有一个UIViewController
,通过以下方式初始化上述UIView
:
myView=[myAnimationView alloc]initWithFrame:(CGRect){{{0,0},320,450}];
[self.view addSubview myView]代码>
所以我拥有的是:一个UIView
类和上面的UIViewController
类。没有别的了
随着CAShapeLayer
动画的制作(它只是一个基本的ca动画,从一个小圆圈缩放到一个更大的圆圈),我希望能够在UIViewController
中获得触发此UIView
我应该在这里使用hitTest:WithEvents:here吗?我试过了,但打了三次电话。返回的点是正确的,但我希望找到一种方法来知道是否正在从容器视图中触摸动画视图。换句话说,我想知道是否正在触摸子视图/子层
总之,以下是我的视图层次结构:
UIViewController
初始化UIView
类并将其添加为子视图。
在这个UIView
类中,它的层被CALayer*layer=(CALayer*)self.layer
设置为CALayer
,而CAShapeLayer
被设置为CALayer
的遮罩,动画被设置在形状层的路径上
我希望能够在UIViewController
是否可以从将其添加为其子视图的UIViewController
中的层设置为CALayer
的视图中由CAShapeLayer设置动画的层获取接触点?请举例说明
非常感谢。问候。我认为CALayer
不是为接收/处理触摸事件而设计的(这与UIView
有很大区别)。
我在ViewController的CAShapeLayer.path
中测试了(长按)触摸,如下所示
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
CGPoint currentPoint = [recognizer locationInView:self.overlayView];
bool didSelect = [self selectPathContainingPoint:currentPoint];
if (didSelect) {
[self updateViews];
}
}
- (BOOL)selectPathContainingPoint:(CGPoint)point {
CALayer * layer = (CALayer *) self.layer;
CAShapeLayer *mask = (CAShapeLayer *)layer.mask;
return CGPathContainsPoint(mask.path, nil, point);
}
但是有一个警告:我的路径没有设置动画。在CAShapeLayer的文档中,如果CAShapeLayer.path
属性在动画期间更新,它也不会显示任何内容。尝试以下操作:
- (void)setup
{
_maskLayer = [CAShapeLayer layer];
_maskLayer.fillColor = [UIColor whiteColor].CGColor;
_maskLayer.path = somePath;
_theLayer.frame = CGRectMake(0, 0, size.width, size.height);
// Apply a mask
_maskLayer.frame = _theLayer.frame;
_theLayer.mask = _maskLayer;
}
- (IBAction)tapOnView:(UITapGestureRecognizer *)sender
{
CGPoint point = [sender locationInView:theView];
CALayer *subLayer = [_theLayer.presentationLayer hitTest:point].modelLayer;
if (subLayer != _theLayer && subLayer != nil) {
// Do something with the sublayer which the user touched
}
}
您必须设置TapGestureRecognitor,我使用Interface Builder完成了这项工作,但如果需要,您可以在代码中完成。确保图层具有正确的边界设置,并且还需要在遮罩上设置边界
请注意,我正在获取presentationLayer并将其转换回真实层。这将使它与动画一起工作
我希望这对你有用