2015-10-16 54 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)