所以我一直在寻找所有,我还没有完全找到我在找什么。触摸CALayer时触发一个动作?
我有一个视图,然后是该视图的子视图。在第二个视图中,我根据我给出的坐标创建了CALayers。我希望能够触摸任何这些CALayers并触发某些事物。
我发现不同的代码看起来像他们可以帮助,但我一直没有能够实现它们。
例如:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if ([touches count] == 1) { for (UITouch *touch in touches) {
CGPoint point = [touch locationInView:[touch view]]; point = [[touch view] convertPoint:point toView:nil];
CALayer *layer = [(CALayer *)self.view.layer.presentationLayer hitTest:point];
layer = layer.modelLayer; layer.opacity = 0.5;
} } }
而且这个....
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// If the touch was in the placardView, bounce it back to the center
if ([touch view] == placardView) {
// Disable user interaction so subsequent touches don't interfere with animation
self.userInteractionEnabled = NO;
[self animatePlacardViewToCenter];
return;
}
}
我还是很值得初学者到这个东西。我想知道是否有人能告诉我如何做到这一点。谢谢你的帮助。
这条线:[self.secondView.layer convertPoint:P toLayer:pointLayer]我收到一个错误,指出:“不兼容类型对'containsPoint'的参数1有任何想法我应该做什么或出了什么问题? – 2010-11-14 19:27:54
您需要确保您将CGPoint传递给containsPoint :.如果您不确定,请使用中间局部变量。 – 2011-12-04 17:39:34