animateWithDuration一个接一个

时间:2016-04-24 05:34:40

标签: ios swift loops uiview animatewithduration

我有一个最初位于屏幕中心的标签。它当前从屏幕的中心转换到右端,然后自动反转回到它的位置。我想让它开始另一个animateWithDuration,以便它从中心返回到屏幕的左侧位置然后自动反向回到该位置并从那之后依次循环。

我已经尝试并成功完成了上半部分工作,但我不知道如何继续第二部分开始中心 - >左转换和循环。

Swift 2.0代码:

   func animateRight()
    {
        UIView.animateWithDuration(1.0, delay: 0.0, options: [ .Autoreverse, .CurveEaseInOut], animations: {
                label.center.x = self.view.frame.width/2
            }, completion: { finished in
                if finished {
                    label.frame.origin.x = 0.0
                    animateLeft()
                }
        })
    }

    func animateLeft()
    {
        UIView.animateWithDuration(1.0, delay: 0.0, options: [ .Autoreverse, .CurveEaseInOut], animations: {
                label.frame.origin.x = (self.view.frame.width/2) * -1
            }, completion: { finished in
                if finished {
                    label.center.x = self.view.frame.width/2
                    animateRight()
                }
        })
    }

    // Start process
    animateRight()

2 个答案:

答案 0 :(得分:1)

您应该使用您创建的持续时间方法调用相同的动画,以便向右设置动画并在完成时调用。像这样:

func animateRight()
{
    UIView.animateWithDuration(1.0, delay: 0.0, options:[], animations: {
        self.label.center.x = self.view.frame.width
        }, completion: { finished in
            if finished {
                self.animateLeft()
            }
    })
}

func animateLeft()
{
    UIView.animateWithDuration(2.0, delay: 0.0, options: [ .Autoreverse, .Repeat, .CurveEaseInOut, .BeginFromCurrentState], animations: {
        self.label.frame.origin.x = 0.0
        }, completion: nil)
}

答案 1 :(得分:0)

  • 调用 defaultSetup 开始
  • animationRight结束时,它会调用animationLeft
  • animationLeft结束时,它会再次调用animationRight来运行动画循环

代码如下:

    func defaultSetup(){
        
        self.animationRight()
    }
    
    func animationRight(){
        
        let transform = CGAffineTransform(scaleX: 1.05, y: 1.05).rotated(by: .pi/180)
        UIView.animate(withDuration: 3.0, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 3.0, options: [.allowUserInteraction, .repeat], animations: { [weak self] in
            guard let `self` = self else { return }
            self.vRight.transform = transform
            
        }, completion: { [weak self] (done) in
            guard let `self` = self else { return }
            self.vRight.transform = .identity
            self.animationLeft()
        })
    }
    
    func animationLeft(){
        let transform = CGAffineTransform(scaleX: 1.05, y: 1.05).rotated(by: .pi/180)
        UIView.animate(withDuration: 3.0, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 3.0, options: [.allowUserInteraction], animations: { [weak self] in
            guard let `self` = self else { return }
            self.vLeft.transform = transform
            
        }, completion: { [weak self] (done) in
            guard let `self` = self else { return }
            self.vLeft.transform = .identity
            self.animationRight()
        })
    }