如何在移动过程中定义 CALayer 在 UIView 中的位置

How define location of CALayer in the UIView during movement

我想制作一个应用程序,让苹果从树上掉下来,用户可以用篮子接住它。当我触摸屏幕时,篮子开始移动,因此它会朝那个方向移动 - 但速度很慢。为此,我使用

-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch* t = [touches anyObject];
    CGPoint p = [t locationInView:self.view];
    if(p.y <self.view.frame.size.height-50)
    {
        p.y = self.view.frame.size.height-50;
    }

    CABasicAnimation *basketmovementAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    [basketmovementAnimation setFromValue:[NSValue valueWithCGPoint:[[self.basketLayer presentationLayer]position]]];
    [basketmovementAnimation setDuration:3.0];

    [self.basketLayer setPosition:p];
    [self.basketLayer addAnimation:basketmovementAnimation forKey:@"basket"];
}

所以问题是在移动过程中如何获取"basket"的坐标?我需要它来与掉落的苹果的坐标进行比较。 提前致谢

CALayer 有一个方法。

- (id)presentationLayer

来自 CALayer Class 参考

The layer object returned by this method provides a close approximation of the layer that is currently being displayed onscreen. While an animation is in progress, you can retrieve this object and use it to get the current values for those animations.