沿着path路径做动画

沿着path路径做动画

 

路径

 

效果

 

源码




//
//  ViewController.m
//  PathAnimation
//
//  Created by YouXianMing on 16/1/26.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic) CGPoint startPoint;
@property (nonatomic) CGPoint endPoint;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 起始点 结束点
    self.startPoint = CGPointMake(0, 150);
    self.endPoint   = CGPointMake(160, 150);

    // 初始化view
    UIButton *redButton       = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    redButton.center          = self.startPoint;
    redButton.backgroundColor = [UIColor redColor];
    [redButton addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:redButton];
    
    // CAKeyframeAnimation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path                 = [self path].CGPath;
    animation.duration             = 2.f;
    animation.timingFunction       = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    redButton.center               = self.endPoint;
    [redButton.layer addAnimation:animation forKey:nil];
}

- (UIBezierPath *)path {

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    
    [bezierPath moveToPoint:    self.startPoint];
    [bezierPath addLineToPoint: CGPointMake(68, 150)];
    [bezierPath addLineToPoint: CGPointMake(83, 107)];
    [bezierPath addLineToPoint: CGPointMake(96, 206)];
    [bezierPath addLineToPoint: CGPointMake(102, 150)];
    [bezierPath addLineToPoint: CGPointMake(116, 150)];
    [bezierPath addLineToPoint: CGPointMake(127, 82)];
    [bezierPath addLineToPoint: CGPointMake(143, 213)];
    [bezierPath addLineToPoint: self.endPoint];

    return bezierPath;
}

- (void)buttonEvent {
    
    // todo
}

@end

细节

 

效果

 

源码


 

//
//  ViewController.m
//  PathAnimation
//
//  Created by YouXianMing on 16/1/26.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic) CGPoint startPoint;
@property (nonatomic) CGPoint endPoint;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // 起始点 结束点
    self.startPoint = CGPointMake(0, 150);
    self.endPoint   = CGPointMake(160, 150);

    // 初始化view
    UIButton *redButton       = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    redButton.center          = self.startPoint;
    redButton.backgroundColor = [UIColor redColor];
    [redButton addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:redButton];
    
    // CAKeyframeAnimation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path                 = [self path].CGPath;
    animation.duration             = 2.f;
    animation.autoreverses         = true;
    animation.repeatCount          = CGFLOAT_MAX;
    animation.timingFunction       = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    [redButton.layer addAnimation:animation forKey:nil];
}

- (UIBezierPath *)path {

    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    
    [bezierPath moveToPoint:    self.startPoint];
    [bezierPath addLineToPoint: CGPointMake(68, 150)];
    [bezierPath addLineToPoint: CGPointMake(83, 107)];
    [bezierPath addLineToPoint: CGPointMake(96, 206)];
    [bezierPath addLineToPoint: CGPointMake(102, 150)];
    [bezierPath addLineToPoint: CGPointMake(116, 150)];
    [bezierPath addLineToPoint: CGPointMake(127, 82)];
    [bezierPath addLineToPoint: CGPointMake(143, 213)];
    [bezierPath addLineToPoint: self.endPoint];

    return bezierPath;
}

- (void)buttonEvent {
    
    // todo
}

@end

细节



要实现Dotween Path完成路径动画后平滑倒播,你可以使用Dotween的Reverse方法来实现。下面是一个示例代码片段,演示了如何使用Reverse方法来实现平滑倒播: ```csharp using UnityEngine; using DG.Tweening; public class PathAnimation : MonoBehaviour { private Tweener tweener; private void Start() { // 在这里创建Dotween路径动画 tweener = transform.DOPath(/* your path */, /* duration */, /* path type */) .SetOptions(/* your options */) .OnComplete(OnPathComplete); } private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { // 按下空格键时开始倒播 ReversePath(); } } private void ReversePath() { if (tweener != null) { // 停止当前路径动画 tweener.Kill(); // 反转路径 tweener.ChangeEndValue(/* your new end value */, /* duration */) .SetOptions(/* your options */) .OnComplete(OnReversePathComplete); } } private void OnPathComplete() { // 路径正向播放完成的回调 Debug.Log("Path animation completed."); } private void OnReversePathComplete() { // 路径倒播完成的回调 Debug.Log("Path animation reversed."); } } ``` 在上面的代码中,你需要根据你的需求来设置路径动画的起点、终点、持续时间、路径类型等参数。通过按下空格键,你可以触发倒播动画。完成路径动画和倒播动画时,对应的回调函数将会被调用。 请注意,你需要先安装并导入Dotween库,然后在代码中引入`using DG.Tweening;`来使用Dotween的相关方法和类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值