2015-10-16 35 views
0

我正在尝试使从左到右移动屏幕的波动起来,并且同时我希望它稍微上下移动。继承人代码:试图在swift中移动x和y字段中的图像

UIView.animateWithDuration(2.5, delay: 0, 
     options: .Repeat | .Autoreverse, animations: { 
      self.wavesImg.center.y += 50 
      self.wavesImg.center.x -= self.wavesImg.image!.size.width/7 
     }, completion: nil) 

目前,它只是执行最后一个动画,所以它只能从左到右移动。我怎样才能让它上下移动呢?

回答

1

为什么不直接适用罪为y给它一个自然的波浪感觉

UIView.animateWithDuration(2.5, delay: 0, 
    options: .Repeat | .Autoreverse, animations: { 
     self.wavesImg.center.y += 50 * sin(self.wavesImg.center.x) 
     self.wavesImg.center.x -= self.wavesImg.image!.size.width/7 
    }, completion: nil) 

我不知道你想怎么处理的价值在罪中去,现在我对待每个x值作为1弧度,因为sin函数具有弧度,但您可能想要改变它以适合您的需要。 (也就是说,如果你想让波从0开始,那么当x是10时,上升到下降到0,你会做类似sin((x/10)* PI)