0

在此先感谢:我有这样的视图层次结构:

我在其中设置了一个UIViewCALayer * 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动画,从一个小圆圈缩放到一个更大的圆圈),我希望能够在UIViewControllerinit中获得接触点UIView

我应该在这里使用 hitTest:WithEvents: 吗?我已经尝试过了,但它被调用了三遍。返回的点是正确的,但我希望找到一种方法来了解是否从容器视图中触摸了动画视图。换句话说,我想知道 subView /subLayer 是否被触摸。

总之,这是我的视图层次结构:

UIViewController初始化UIView类并将其添加为子视图。在这个UIView类中,它的层被设置为CALayerby CALayer * layer = (CALayer *) self.layerCAShapeLayer被设置为蒙版,CALayer动画被设置在形状层的路径上。

我希望能够接触到里面的图层UIViewController

是否可以从由 CAShapeLayer 在其图层设置为将其添加为其子视图的视图中设置动画的图层中获取触摸CALayerUIViewController?请举例说明。

首先十分感谢。问候。

4

2 回答 2

0

Try something like this:

- (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
    }
}

You will have to setup your TapGestureRecognizer, I did this using Interface Builder but you can do it in code if you want. Be sure that your layers have proper bounds setup, and you also need to set the bounds on your mask.

Notice that I am getting the presentationLayer and the converting it back to the real layer. This will cause it to work with animations.

I hope this works for you.

于 2013-09-06T01:04:20.763 回答
0

我认为CALayer不是为接收/处理触摸事件而设计的(这是与 的一大区别UIView)。CAShapeLayer.path我测试了 ViewController中的(长按)触摸,例如

- (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.path在 CAShapeLayer 的文档中,如果属性在动画期间更新,它也不会说明任何内容。

于 2013-06-19T14:55:16.410 回答