2015-12-08 20 views
0

我試圖做一個小遊戲。並且動畫有一些問題。我新來Swift。所以讓我們看看。我創建了一個UIImageView圖片,並且想要將這張圖片的動畫出現在屏幕上的不同位置。我相信該算法將如下所示: 無限循環{0} {0} {0} {0} 1-GetRandomPlace 不透明度從0到1並返回(與平滑過渡) } 看起來很簡單,但我不明白該怎麼做在Xcode中正確。 這是我的測試代碼,但它看起來沒用 謝謝你的幫助和抱歉,如果已經有這個問題,我找不到它。Swift UIView出現在不同的地方

class ViewController: UIViewController { 

@IBOutlet weak var BackgroundMainMenu:UIImageView! 
@IBOutlet weak var AnimationinMenu: UIImageView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

// MoveBackgroundObject(AnimationinMenu) 
// AnimationBackgroundDots(AnimationinMenu, delay: 0.0) 

//  self.AnimationinMenu.alpha = 0 
//   
//  UIImageView.animateWithDuration(3.0, 
//   delay: 0.0, 
//   options: UIViewAnimationOptions([.Repeat, .CurveEaseInOut]), 
//   animations: { 
//    self.MoveBackgroundObject(self.AnimationinMenu) 
//    self.AnimationinMenu.alpha = 1 
//    self.AnimationinMenu.alpha = 0 
//   }, 
//   completion: nil) 

} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

override func viewWillAppear(animated: Bool) { 
    AnimationBackgroundDots(AnimationinMenu, delay: 0.0) 

} 


func ChangeOpacityto1(element: UIImageView){ 
    element.alpha = 0 
    UIView.animateWithDuration(3.0) { 
     element.alpha = 1 
    } 
} 

func ChangeOpacityto0(element: UIImageView){ 
    element.alpha = 1 
    UIView.animateWithDuration(3.0){ 
     element.alpha = 0 
    } 
} 

func AnimationBackgroundDots(element: UIImageView, delay: Double){ 
    element.alpha = 0 
    var z = 0 
    while (z<4){ 
     MoveBackgroundObject(AnimationinMenu) 
      UIImageView.animateWithDuration(3.0, 
      animations: { 
       element.alpha = 0 
       element.alpha = 1 
       element.alpha = 0 
      }, 
      completion: nil) 
     z++ 
    } 
} 

func MoveBackgroundObject(element: UIImageView) { 
    // Find the button's width and height 
    let elementWidth = element.frame.width 
    let elementHeight = element.frame.height 

    // Find the width and height of the enclosing view 
    let viewWidth = BackgroundMainMenu.superview!.bounds.width 
    let viewHeight = BackgroundMainMenu.superview!.bounds.height 

    // Compute width and height of the area to contain the button's center 
    let xwidth = viewWidth - elementWidth 
    let yheight = viewHeight - elementHeight 

    // Generate a random x and y offset 
    let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth))) 
    let yoffset = CGFloat(arc4random_uniform(UInt32(yheight))) 

    // Offset the button's center by the random offsets. 
    element.center.x = xoffset + elementWidth/2 
    element.center.y = yoffset + elementHeight/2 
} 

}

+0

它做什麼或不做什麼與你想要做什麼? – blm

回答

0

的問題是在AnimationBackgroundDots

您立即在同一視圖上創建4個動畫,但每次只能運行一個動畫。你需要做的是等到一個動畫完成(淡入或淡出),然後再開始一個新動畫。

此外,animations閉包用於設置您希望視圖生成動畫的狀態。它着眼於你的觀點是如何開始的,運行animations,然後再次查看視圖並計算出兩者之間的動畫。在你的情況下,UIImageViewalpha從0開始,然後當animations運行時,alpha最終爲0,所以沒有任何動畫。您無法創建動畫應採用的所有步驟。

想要您做到這一點,請移動您的視圖並開始淡入動畫。淡入的完成關閉應該開始淡出動畫。淡出的完成關閉應該重新開始整個過程​​。它可能看起來像這樣。

func AnimationBackgroundDots(element: UIImageView, times: Int) { 
    guard times > 0 else { 
     return 
    } 

    MoveBackgroundObject(element) 
    element.alpha = 1 

    // Fade in 
    UIView.animateWithDuration(3.0, animations: { 
     element.alpha = 1 
    }, completion: { finished in 
     // Fade Out 
     UIView.animateWithDuration(3.0, animations: { 
      element.alpha = 0 
     }, completion: { finished in 
      // Start over again 
      self.AnimationBackgroundDots(element, times: times-1) 
     }) 
    }) 
} 

你叫也要看使用keyframe animations,但這種情況下是足夠的簡單使用他們那裏有沒有好處。


另外作爲一個附註。 swift中的函數命名慣例是以小寫字母開頭,因此AnimationBackgroundDots應該是animationBackgroundDots