How to animate one image over the unusual curve path in my iPad application?

前端 未结 1 1567
渐次进展
渐次进展 2021-01-07 14:53

I have two separate images.

  1. CurvedPath image (Attached Below)
  2. PersonImage

\"ente

1条回答
  •  一生所求
    2021-01-07 15:12

    Moving along a path

    As long as you can get the exact path that you want to animate the image align you can do it using Core Animation and CAKeyframeAnimation.

    Create a key frame animation for the position property and set it do animate along your path

    CAKeyframeAnimation *moveAlongPath = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    [moveAlongPath setPath:myPath]; // As a CGPath
    

    If you created your path as a UIBezierPath then you can easily get the CGPath by calling CGPath on the bezier path.

    Next you configure the animation with duration etc.

    [moveAlongPath setDuration:5.0]; // 5 seconds
    // some other configurations here maybe...
    

    Now you add the animation to your imageView's layer and it will animate along the path.

    [[myPersonImageView layer] addAnimation:moveAlongPath forKey:@"movePersonAlongPath"];
    

    If you've never used Core Animation before, you need to add QuartzCore.framework to your project and add #import at the top of your implementation.

    Creating a UIBezierPath

    If you don't know what a bezier path is, look at the Wikipedia site. Once you know your control points you can create a simple bezier path like this (where all the points are normal CGPoints):

    UIBezierPath *arcingPath = [UIBezierPath bezierPath];
    [arcingPath moveToPoint:startPoint];
    [arcingPath addCurveToPoint:endPoint 
                  controlPoint1:controlPoint1 
                  controlPoint2:controlPoint2];
    CGPathRef animationPath = [arcingPath CGPath]; // The path you animate along
    

    Zooming up

    To achieve the zoom effect you can apply a similar animation using a CABasicAnimation for the transform of the layer. Simply animate from a 0-scaled transform (infinitely small) to a 1-scaled transform (normal size).

    CABasicAnimation *zoom = [CABasicAnimation animationWithKeyPath:@"transform"];
    [zoom setFromValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.0, 0.0, 1.0);];
    [zoom setToValue:[NSValue valueWithCATransform3D:CATransform3DIdentity];
    [zoom setDuration:5.0]; // 5 seconds
    // some other configurations here maybe...
    [[myPersonImageView layer] addAnimation:zoom forKey:@"zoomPersonToNormalSize"];
    

    Both at the same time

    To have both animations run at the same time you add them to an animation group and add that to the person image view instead. If you do this then you configure the animation group (with duration and such instead of the individual animations).

    CAAnimationGroup *zoomAndMove = [CAAnimationGroup animation];
    [zoomAndMove setDuration:5.0]; // 5 seconds
    // some other configurations here maybe...
    [zoomAndMove setAnimations:[NSArray arrayWithObjects:zoom, moveAlongPath, nil]];
    [[myPersonImageView layer] addAnimation:zoomAndMove forKey:@"bothZoomAndMove"];
    

    0 讨论(0)
提交回复
热议问题