Search code examples
iosobjective-ccore-animationcalayercabasicanimation

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


Solution

  • CALayer has a method for this.

    - (id)presentationLayer
    

    From the CALayer Class Reference

    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.