0

我有一个添加为UIWebView. 我希望能够触摸并拖动图层。在添加 之前UIWebView,我可以毫无问题地触摸和拖动图层。

这是我创建图层并将其添加为子图层的地方

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

这是我的moveStar方法

- (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"];

}

到现在为止还挺好。星星落在屏幕上,落在中间上方一点。但是当我尝试触摸星星时,什么也没有发生。touchesBegan永远不会因为某种原因被调用。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.aboutWebView];
    [starLayer setPosition:point];
}

当它是子层时我不能触摸星星的原因是aboutWebView因为触摸报告给视图而不是图层?

有没有人对如何使星星成为aboutWebView可触摸的子层有任何建议?是否可以?

谢谢!

4

1 回答 1

0

在您的图层中...尝试将UIGestureRecognizer属性设置cancelsTouchesInView为 NO。

“将此值设置为 NO 指示识别器将所有触摸传递到底层视图,即使它已识别序列”

类似的东西:

UIGestureRecognizer *touches = [UIGestureRecognizer alloc];
    //or
    //UIGestureRecognizer *touches = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(touchesAction:)];

    touches.enabled=YES;
    [yourLayer addGestureRecognizer:touches];
    touches.cancelsTouchesInView=NO;
于 2013-01-17T11:44:31.787 回答