简体   繁体   English

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

[英]How define location of CALayer in the UIView during movement

I have want to make an application where apples fall down from the tree and user can catch it with the basket. 我要创建一个应用程序,其中苹果从树上掉下来,用户可以用篮子抓住它。 Basket starts moving when I touch the screen so it moves in that direction - but slowly. 当我触摸屏幕时,篮子开始移动,因此它朝那个方向移动-但速度很慢。 For this I use 为此,我使用

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

So the question is how to get coordinates of "basket" during movement? 那么问题是如何在运动过程中获取“篮子”的坐标? I need it to compare with the coordinates of fallen apples. 我需要它与下落的苹果的坐标进行比较。 Thanks in advance 提前致谢

CALayer has a method for this. CALayer为此提供了一种方法。

- (id)presentationLayer

From the CALayer Class Reference 从CALayer类参考

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. 在动画制作过程中,您可以检索该对象并使用它获取那些动画的当前值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM