jjzjj

swift - CABasicAnimation 反向(向后)

coder 2023-09-08 原文

我对这个简单的线条动画有点吃力。我想出了如何暂停它,但我需要的是能够从我调用函数 resetAnimation() 的那一刻起将动画反转回起点。

let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
    let pathLayer = CAShapeLayer()

     func lineAnimation() {

        let path = UIBezierPath()
        let screenWidth = self.view.bounds.width
        let screenHeight = self.view.bounds.height
        path.moveToPoint(CGPointMake(screenWidth, screenHeight / 2))
        path.addLineToPoint(CGPointMake(screenWidth - screenWidth, screenHeight / 2))

        self.pathLayer.frame = self.view.bounds
        self.pathLayer.path = path.CGPath
        self.pathLayer.strokeColor = UIColor.whiteColor().CGColor
        self.pathLayer.fillColor = nil
        self.pathLayer.lineWidth = 3.0
        self.pathLayer.lineCap = kCALineCapRound
        self.pathLayer.speed = 1

        self.view.layer.addSublayer(pathLayer)

        self.pathAnimation.duration = 5.0
        self.pathAnimation.fromValue = 0.0
        self.pathAnimation.toValue = 1.0

        pathLayer.addAnimation(pathAnimation, forKey: "animate")

    }

    func pauseAnimation() {

        let pausedTime = pathLayer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        pathLayer.speed = 0
        pathLayer.timeOffset = pausedTime

    }

    func resetAnimation() {


    }

最佳答案

您只需要创建一个新动画并删除旧动画。新动画的起点将是表示层中该属性的当前值。我还建议将形状图层的框架设置为实际贝塞尔曲线形状的边界,而不是整个 View - 当您开始四处移动和缩放/旋转等时,这是一个好习惯。否则,您将面临一堆时髦的转换或 anchor 更改。

这是我要做的:

let pathLayer = CAShapeLayer()

// first, separate your drawing code from your animation code.
// this way you can call animations without instantiating new objects
func drawLine() {
    let path = UIBezierPath()
    // draw your path with no position translation.. move the layer
    path.moveToPoint(CGPointMake(view.bounds.width, 0))
    path.addLineToPoint(CGPointMake(0, 0))
    pathLayer.frame = path.bounds
    // this line sets the position of the layer appropriately
    pathLayer.position = view.bounds.width - pathLayer.bounds.width / 2
    pathLayer.path = path.CGPath
    pathLayer.strokeColor = UIColor.whiteColor().CGColor
    pathLayer.fillColor = nil
    pathLayer.lineWidth = 3.0
    pathLayer.lineCap = kCALineCapRound
    view.layer.addSublayer(pathLayer)
}

func lineAnimation() {
    let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
    pathAnimation.duration = 5.0
    pathAnimation.fromValue = 0.0
    pathAnimation.toValue = 1.0
    pathLayer.addAnimation(pathAnimation, forKey: "strokeEnd")

}

func reverseAnimation() {
    let revAnimation = CABasicAnimation(keyPath: "strokeEnd")
    revAnimation.duration = 5.0
    // every Core Animation has a 'presentation layer' that contains the animated changes
    revAnimation.fromValue = pathLayer.presentationLayer()?.strokeEnd
    revAnimation.toValue = 0.0
    pathLayer.removeAllAnimations()
    pathLayer.addAnimation(revAnimation, forKey: "strokeEnd")
}

请记住,您还需要设置属性以便保留动画的结束值。

关于swift - CABasicAnimation 反向(向后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163686/

有关swift - CABasicAnimation 反向(向后)的更多相关文章

  1. ruby - 如何在 Ruby 中创建数字列表并将其反向附加到它 - 2

    给定一个最小整数和最大整数,我想创建一个数组,它从最小值到最大值以二为单位计数,然后倒退(再次以二为单位,重复最大数)。例如,如果最小数是1,最大数是9,我想要[1,3,5,7,9,9,7,5,3,1].我试图尽可能简洁,这就是我使用单行代码的原因。在Python中,我会这样做:range(1,10,2)+range(9,0,-2)在我刚刚开始学习的Ruby中,到目前为止我所想到的是:(1..9).inject([]){|r,num|num%2==1?r这行得通,但我知道必须有更好的方法。这是什么? 最佳答案 (1..9).step

  2. 没有正则表达式反向引用的 Ruby 字符串子 - 2

    我正在尝试用Ruby做一个简单的字符串子。sub()的第二个参数是一长段精简的JavaScript,其中包含正则表达式。此字符串中正则表达式中的反向引用似乎影响了sub的结果,因为被替换的字符串(即第一个参数)出现在输出字符串中。例子:input="stringishere"output=input.sub("","\&")我希望输出为:"string\&ishere"不是:"string&ishere"或者如果转义正则表达式"stringishere"基本上,我想要一些方法来做一个完全没有正则表达式结果的字符串子-只是一个简单的字符串替换。 最佳答案

  3. Ruby 可枚举反向检测 - 2

    假设我有以下数组:views=[{:user_id=>1,:viewed_at=>'2012-06-2917:03:28-0400'},{:user_id=>1,:viewed_at=>'2012-06-2917:04:28-0400'},{:user_id=>2,:viewed_at=>'2012-06-2917:05:28-0400'},{:user_id=>3,:viewed_at=>'2012-06-2917:06:28-0400'},{:user_id=>1,:viewed_at=>'2012-06-2917:07:28-0400'},{:user_id=>1,:viewed

  4. ruby - 如何使用 Ruby 中的特定步骤迭代反向范围? - 2

    我可以这样迭代(0..10).step(2){|v|putsv}但是,由于反向范围等于空范围,我不能这样迭代(10..0).step(2){|v|putsv}它不会给我带来任何好处。当然,我可以像这样向后迭代10.downto(0){|v|putsv}但downto方法不允许我设置除默认1之外的其他步骤。这是非常基本的东西,所以我想应该有一个内置的方法来做到这一点,我不知道。 最佳答案 你为什么不使用Numeric#step:来自文档:Invokesblockwiththesequenceofnumbersstartingatnum

  5. ruby - 在 Ruby 中反向 DNS? - 2

    我处在一个有很多计算机的环境中适当盘点。基本上,没有人知道哪个IP对应哪个mac地址和哪个主机名。所以我写了以下内容:#ThisscriptgoesdowntheentireIPrangeandattemptsto#retrievetheHostnameandmacaddressandoutputsthem#intoafile.Yay!require"socket"TwoOctets="10.26"defcomputer_exists?(computerip)system("ping-c1-W1#{computerip}")enddefappend_to_file(line)file=

  6. ruby - 使用分组时如何使用 gsub 在 Ruby 正则表达式 (regex) 中反向引用? - 2

    我想修补一些从网页中提取的文本数据。示例:t="Firstsentence.Secondsentence.Thirdsentence."第二句末尾点后没有空格。这表明第3句话在原始文档中位于单独的一行(在br标记之后)。我想使用此正则表达式将“\n”字符插入适当的位置并修补我的文本。我的正则表达式:t2=t.gsub(/([.\!?])([A-Z1-9])/,$1+"\n"+$2)但不幸的是它不起作用:“NoMethodError:undefinedmethod‘+’fornil:NilClass”如何正确反向引用匹配的组?在MicrosoftWord中非常简单,我只需使用\1和\2符

  7. ruby-on-rails - 如何反向循环遍历日期范围? - 2

    我有一个日期范围,我希望能够反向循环。请提供以下内容,我将如何完成此操作,标准的Range运算符似乎无法正常工作。>>sd=Date.parse('2010-03-01')=>Mon,01Mar2010>>ed=Date.parse('2010-03-05')=>Fri,05Mar2010>>(sd..ed).to_a=>[Mon,01Mar2010,Tue,02Mar2010,Wed,03Mar2010,Thu,04Mar2010,Fri,05Mar2010]>>(ed..sd).to_a=>[]如您所见,范围运算符从头到尾都能正常工作,但不能从头到尾正常工作。

  8. ruby - 如何使用正则表达式和反向引用编写 Ruby switch 语句(case...when)? - 2

    我知道我可以编写Rubycase语句来检查与正则表达式的匹配。但是,我想在返回语句中使用匹配数据。像这样的半伪代码:foo="10/10/2011"casefoowhen/^([0-9][0-9])/print"themonthis#{match[1]}"elseprint"somethingelse"end我怎样才能做到这一点?谢谢!请注意:我知道我永远不会在上述简单情况下使用switch语句,但这只是一个示例。实际上,我试图实现的是为一个可以用各种方式编写的日期匹配许多潜在的正则表达式,然后相应地使用Ruby的Date类对其进行解析。 最佳答案

  9. javascript - 在谷歌地图反向地理编码中获取明确的城市名称 - 2

    使用GoogleMapsGeocodingAPI,我能够获取特定坐标的格式化地址。为了获得确切的城市名称,我正在执行以下操作:$.ajax({url:'http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&sensor=false',success:function(data){varformatted=data.results;varaddress_array=formatted[6].formatted_address.split(',');varcity=address_array[0];

  10. Javascript - 将值映射到键(反向对象映射) - 2

    我想反转对象的映射(可能有重复值)。示例:constcity2country={'Amsterdam':'Netherlands','Rotterdam':'Netherlands','Paris':'France'};reverseMapping(city2country)应该输出:{'Netherlands':['Amsterdam','Rotterdam'],'France':['Paris']}我提出了以下天真的解决方案:constreverseMapping=(obj)=>{constreversed={};Object.keys(obj).forEach((key)=>{r

随机推荐