2017-03-03 87 views
0

我试图将UIImageView移动到顶部,底部,左侧和右侧。动画应该四次四次。中心图像向上移动,然后移到左侧位置和右侧位置。我怎样才能使用UIAnimation Swift 3来实现这一点?Swift3中的UIView动画

​​

如何使用动画将图像移动到顶部,左侧,右侧和底部?

+0

您可以将UIImageView的中心动画到您的首选位置 – Mannopson

回答

0
UIView.animate(withDuration: 1, delay:0, animations: { 
     //top 
     self.tempVW.frame.origin.y = self.tempVW.frame.origin.y - 100 
    }, completion: {completion in 
     UIView.animate(withDuration: 1, delay:0, animations: { 
      //left 
      self.tempVW.frame.origin.x = self.tempVW.frame.origin.x - 100 

     }, completion: {completion in 
      UIView.animate(withDuration: 1, delay:0, animations: { 
       //bottom 
       self.tempVW.frame.origin.y = self.tempVW.frame.origin.y + 100 

      }, completion: {completion in 
       UIView.animate(withDuration: 1, delay:0, animations: {      //right 
        self.tempVW.frame.origin.x = self.tempVW.frame.origin.x + 100 
       }, completion: {completion in 


       }) 
      }) 
     }) 

    }) 
0
UIView.animate(withDuration: 1, delay:0, animations: { 
     //top 
     self.imageView.frame.origin.y = self.imageView.frame.origin.y - 100 
    }, completion: {completion in 
     UIView.animate(withDuration: 1, delay:0, animations: { 
      //bottom 
      self.imageView.frame.origin.y = self.imageView.frame.origin.y + 100 
     }, completion: {completion in 
      UIView.animate(withDuration: 1, delay:0, animations: { 
       //left 
       self.imageView.frame.origin.x = self.imageView.frame.origin.x - 100 

      }, completion: {completion in 
       UIView.animate(withDuration: 1, delay:0, animations: { 

       }, completion: {completion in 
        //right 
        self.imageView.frame.origin.x = self.imageView.frame.origin.x + 100 
       }) 
      }) 
     }) 

    }) 
2

您应该使用关键帧动画这一点,因为它允许你指定的动画顺序。以下是一个基本示例:

let animationDuration = 2.0 
let position: CGFloat = 50.0 
let viewToAnimate = UIImageView() 

UIView.animateKeyframes(withDuration: animationDuration, delay: 0.0, options: .calculationModeLinear, animations: { 
    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/3, animations: { 
     viewToAnimate.frame.origin.y = viewToAnimate.frame.origin.y + position 
    }) 
    UIView.addKeyframe(withRelativeStartTime: 1/4, relativeDuration: 1/4, animations: { 
     viewToAnimate.frame.origin.y = viewToAnimate.frame.origin.y - (position * 2) 
    }) 
    UIView.addKeyframe(withRelativeStartTime: 2/4, relativeDuration: 1/4, animations: { 
     viewToAnimate.frame.origin.x = viewToAnimate.frame.origin.x - position 
    }) 
    UIView.addKeyframe(withRelativeStartTime: 3/4, relativeDuration: 1/4, animations: { 
     viewToAnimate.frame.origin.x = viewToAnimate.frame.origin.x + (position * 2) 
    }) 
}, completion: { completed in 

})