I have a layer that I have added as a sublayer of a UIWebView. I would like to be able to touch and drag the layer around. Before I added the UIWebView, I was able to touch and drag around the layer without a problem.
Here’s where I create the layer and add it as a sublayer
- (void)viewDidLoad
{
[super viewDidLoad];
starLayer = [CALayer layer];
starLayer.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage
imageNamed:@"star"]].CGColor;
starLayer.shadowOffset = CGSizeMake(0, 3);
starLayer.shadowRadius = 5.0;
starLayer.shadowColor = [UIColor blackColor].CGColor;
starLayer.shadowOpacity = 0.8;
starLayer.frame = CGRectMake(235.0, 200.0, 35.0, 35.0);
[self.aboutWebView.layer addSublayer:starLayer];
[self moveStar];
}
Here’s my moveStar method
- (void)moveStar
{
CAKeyframeAnimation *move = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCGPoint:CGPointMake(250.0, 50.0)]];
[values addObject:[NSValue valueWithCGPoint:CGPointMake(250.0, 250.0)]];
[move setValues:values];
[move setDuration:1.0];
[move setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
// Add the animation to the layer to be animated
[starLayer addAnimation:move forKey:@"moveAnimation"];
}
So far so good. The star falls onto the screen and lands a bit above the middle. But when I try and touch the star nothing happens. touchesBegan never gets called for some reason.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.aboutWebView];
[starLayer setPosition:point];
}
Is the reason I can’t touch the star when it’s a sublayer of aboutWebView because a touch is reported to a view, not a layer?
Does anyone have any suggestions on how to make the star as a sublayer of aboutWebView touchable? Is it possible?
Thank you!
In your layer… try to set the
UIGestureRecognizerpropertycancelsTouchesInViewto NO.“Setting this value to NO instructs the recognizer to deliver all touches to the underlying view, even when it has recognized the sequence”
something like that: