2015-09-10 233 views
2

我试图按下按钮时旋转和图像180度。现在我可以让它旋转180度,但是当我再次按下按钮时,图像不会回转。我觉得我错过了一些简单的东西。旋转按钮上的图像按

if语句正在工作,我只是简化了代码,只留下了不工作的部分。

 if(self.dropdownConstraint.constant == -78) 
    { 
     //roate arrow up 
     UIView.animateWithDuration(0.5, animations: { 
      self.arrow.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI))/180.0) 
      self.view.layoutIfNeeded() 
     }) 
    } 
    else 
    { 
     //roate arrow down 
     UIView.animateWithDuration(0.5, animations: { 
      self.arrow.transform = CGAffineTransformMakeRotation((-180.0 * CGFloat(M_PI))/-180.0) 
      self.view.layoutIfNeeded() 
     })    
    } 

在此先感谢!

回答

3

变换不是累积的,所以第一个旋转180度(从0开始),第二个旋转-180度(也从零开始),看起来是相同的。只需修改逆变换设置旋转回零:

UIView.animateWithDuration(0.5, animations: { 
     self.arrow.transform = CGAffineTransformMakeRotation(0) 
     self.view.layoutIfNeeded() 
    }) 
+0

感谢我认为它必须是简单的东西 – icekomo

0

将转化为身份撤消以前的变换:

UIView.animateWithDuration(0.5, animations: { 
    self.arrow.transform = CGAffineTransformIdentity; 
    self.view.layoutIfNeeded() 
})