提前感谢:我有一个这样的视图层次结构:
我在其中设置了一个UIView:CALayer * layer = (CALayer*) self.layer
在这个类中,我有一个CAShapeLayer,它被设置为CALayer的掩码。
它的动画效果很好,没问题。
我有一个初始化上面的UIView的UIViewController:
myView = [myAnimationView alloc] initWithFrame:(CGRect){{0, 0}, 320, 450}]; [self.view addSubview myView];
所以我拥有的是:一个UIView类和上面的UIViewController类。没别的了。
作为CAShapeLayer动画(它只是一个基本的ca动画,从一个小圆圈缩放到一个更大的圆圈),我希望能够在这个UIView中获得UIViewController中的接触点
我应该使用hitTest:WithEvents: here吗?我试过了,但它被调用了三次。返回的点是正确的,但我希望找到一种方法来了解容器视图中是否正在触摸动画视图。换句话说,我想知道subView /subLayer是否被触摸到了。
总之,以下是我的视图层次结构:
UIViewController初始化UIView类并将其添加为subView。在这个UIView类中,它的层被设置为CALayer by CALayer * layer = (CALayer *) self.layer,CAShapeLayer被设置为CALayer的掩码,动画被设置在shape层的路径上。
我希望能够在UIViewController中接触到这一层
有没有可能从一个层被设置为CALayer的视图内的一个由CAShapeLayer设置动画的层中获取接触点,该层是从一个将其添加为其子视图的UIViewController中获得的?请举例说明。
非常提前谢谢你。致以问候。
发布于 2013-06-19 22:55:16
我认为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属性在动画过程中更新,它也不会显示任何内容。
发布于 2013-09-06 09:04:21
尝试如下所示:
- (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
}
}你必须设置你的TapGestureRecognizer,我是用接口生成器做的,但是如果你愿意,你也可以用代码来做。请确保您的图层有适当的边界设置,您还需要设置您的蒙版的边界。
请注意,我正在获取presentationLayer并将其转换回真正的层。这将使它与动画一起工作。
我希望这对你有用。
https://stackoverflow.com/questions/17158351
复制相似问题