iOS动画暂停和继续

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS动画暂停和继续相关的知识,希望对你有一定的参考价值。

参考技术A 执行动画过程中暂停和继续上次动画的状态继续执行动画,需要用到layer.speed 和 layer.timeOffset, layer.beginTime.
基本做法就是记录暂停时的动画时间,然后继续动画时将开始时间设置为上次暂停的时间。下面是个旋转动画的暂停和继续。

unity游戏的开始和暂停 计时器

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 实现控制 游戏的暂停和继续功能
/// 对于动画暂停可以 mMoveTween.timeScale = timeScale
/// </summary>
public class Timer : MonoBehaviour

    public event Action TimerUpdate;
    public MyTime MyTime1;
    public MyTime MyTime2;
    // Start is called before the first frame update
    void Start()
    
        int count = 1;
        MyTime myTime1 = new MyTime(() =>  print(1); count++; , 5f, -1, false);
        TimerUpdate += myTime1.Update;
        myTime1.Start();
        MyTime1 =myTime1;
        //int count2 = 1;
        //MyTime myTime2 = new MyTime(() =>  print(2); count2--; , 5, 4, false);
        //TimerUpdate += myTime2.Update;
        //myTime2.Start();
        //MyTime2 = myTime2;
    

    // Update is called once per frame
    void Update()
    
        TimerUpdate();

        if (Input.GetKeyDown(KeyCode.W))
        
            MyTime1.Stop(); //MyTime2.Stop();
        

        if (Input.GetKeyDown(KeyCode.S))
        
            MyTime1.Continue();// MyTime2.Continue();
        
    


public class MyTime

    private Action _fun;//需要执行的函数
    private float _duration;//时间间隔
    private float _time;//存储时间间隔  注意最小间隔
    private int _loop;//循环次数
    private bool _unscaled;//是否时间缩放
    public MyTime(Action fun,float duration,int loop,bool unscaled)
    
        _fun = fun;
        _duration = _time = duration;
        _loop = loop;
        _unscaled = unscaled;
    

    public void Reset(Action fun, float duration, int loop, bool unscaled)
    
        _fun = fun;
        _duration = _time = duration;
        _loop = loop;
        _unscaled = unscaled;
        _isEnding = false;
        _running = true;
    

    private bool _isEnding = false;//是否结束
    private bool _running = false;//是否运行
    public void Start()
    
        _running = true;
    

    public void End()
    
        _isEnding = true;
        _running = false;
    

    public void Stop()
    
        if (!_isEnding)
        
            _running = false;
        
    

    public void Continue()
    
        if (!_isEnding)
        
            _running = true;
            
    


    public void Update()
    
        if (!_running) return;

        float delta = _unscaled ? Time.unscaledDeltaTime : Time.deltaTime;
        _time -= delta;
        if (_time <= 0)
        
            
            if (_loop > 0)
            
                _loop--;
                _time += _duration;
            

            if (_loop == 0)
            
                End();
            
            else if (_loop < 0)
            
                _time += _duration;
            
            _fun();
        

    

以上是关于iOS动画暂停和继续的主要内容,如果未能解决你的问题,请参考以下文章

iOS 变形播放/暂停动画

滚动ios时数字增量动画

iOS学习Objective-c OC录音

带有延迟和持续时间 Ios 的动画标签

iOS学习笔记10-UIView动画

iOS:删除 UINavigationBar 动画