Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAKeyframeanimation move UIView along path

Here is my code to move UIview 30px down then up to y=10 but this animation is not working. This is my first attempt to create CAKeyframeAnimation so can anybody help me write it correctly. Also I want my object not to comeback to original place but stay there where animation ended.

CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, self.logo.frame.origin.y, self.logo.frame.size.width, self.logo.frame.size.height));
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, self.logo.frame.origin.y-30, self.logo.frame.size.width, self.logo.frame.size.height));
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, 100, self.logo.frame.size.width, self.logo.frame.size.height));


    CAKeyframeAnimation* AniLoc = [CAKeyframeAnimation animationWithKeyPath:@"frame"];
    AniLoc.path = thePath;
    AniLoc.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    AniLoc.keyTimes= [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
                      [NSNumber numberWithFloat:0.3f],
                      [NSNumber numberWithFloat:1.0f],nil];
    AniLoc.duration = 2.0;

    CFRelease(thePath);

    [self.logo.layer addAnimation:AniLoc forKey:nil];
like image 682
Ayaz Alavi Avatar asked Nov 14 '22 12:11

Ayaz Alavi


1 Answers

I'm not quite sure why your method is not working, but I can suggest another solution. Instead of driving the frame, you can change the position key to move the UIView:

    CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathMoveToPoint(thePath, NULL, self.logo.frame.origin.x, self.logo.frame.origin.y); // initial point, notice the function
    CGPathAddLineToPoint(thePath, NULL, self.logo.frame.origin.x - 30, self.logo.frame.origin.y);
    CGPathAddLineToPoint(thePath, NULL, 100, self.logo.frame.origin.y);

    CAKeyframeAnimation* AniLoc = [CAKeyframeAnimation animationWithKeyPath:@"position"]; // notice key change

    // rest is the same
    AniLoc.path = thePath;
    AniLoc.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    AniLoc.keyTimes= [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
                      [NSNumber numberWithFloat:0.3f],
                      [NSNumber numberWithFloat:1.0f],nil];
    AniLoc.duration = 2.0;

    CFRelease(thePath);

    [self.logo.layer addAnimation:AniLoc forKey:nil];

I hope this helps you.

like image 196
Alexander Avatar answered Nov 16 '22 04:11

Alexander