2016-04-23 217 views
3

我有一个像下拉的图像。最初它会看起来像下拉图像所以当用户按下拉列表时,会显示一些选项。所以我需要的是,当下拉是真实的。我的意思是,当用户按下下拉图像,当选项列表显示时,我需要显示下拉的图像180度。像是当下拉是假的需要将图像显示为正常位置。将图像旋转180度

这种方式是正确的,而不是使用一个更多的图像?我使用SWIFT 2.2

更新时间:

@IBAction func dropBtnPress(sender: AnyObject) { 

      if dropDown.hidden { 
       dropDown.show() 
       UIView.animateWithDuration(0.0, animations: { 
        self.image.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI))/180.0) 
       }) 
      } else { 
       dropDown.hide() 
    //   UIView.animateWithDuration(2.0, animations: { 
    //    self.image.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI))/-180.0) 
    //   }) 
      } 
     } 

    } 
+1

'(180 * X)/ 180 = x'下降(180.0 * CGFloat(M_PI))/ 180.0',并使用CGFloat(M_PI)' – Fogmeister

回答

14

旋转图像,你可以使用这个片段:

UIView.animateWithDuration(2.0, animations: { 
    self.imageView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI)) 
}) 

可以调整动画秒(目前为2.0)。

要设置图像再次使用下面的代码:

UIView.animateWithDuration(2.0, animations: { 
    self.imageV.transform = CGAffineTransform.identity 
}) 

斯威夫特4.x的版本:

旋转:

UIView.animate(withDuration: 2.0, animations: { 
    self.imageView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi)) 
}) 

图像集来的正常状态:

UIView.animate(withDuration: 2.0, animations: { 
    self.imageView.transform = CGAffineTransform.identity 
}) 
+0

其工作。当我按下时,图像变为180度。但是当下拉被隐藏时,它不会改变到正常位置。为什么? – user5513630

+0

@ user5513630,检查更新以使后退按钮再次工作。 –

3

首先,如果你想旋转180度,那必须转化为弧度。 180度弧度为pi。 360度将是2 * pi180 * pi会使您的动画在一秒钟内旋转90次,并以原始方向结束。

你可以做这样的事情,

let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation") 
rotationAnimation.fromValue = 0.0 
rotationAnimation.toValue = M_PI 
rotationAnimation.duration = 1.0 

self.arrowImageView.layer.addAnimation(rotationAnimation, forKey: nil) 

希望这将有助于:)

5

斯威夫特3

UIView.animate(withDuration:0.1, animations: { 
    self.arrowIcon.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat(Double.pi))/180.0) 
})