Swift UIView出现在不同的地方

时间:2015-12-08 18:59:32

标签: ios xcode swift animation uiview

我试图制作一个小游戏。并且动画存在一些问题。我是Swift的新手。所以我们来看看吧。我创建了一个UIImageView图片,并希望将此图片的动画显示在屏幕上的不同位置。我相信算法看起来像这样: 无限循环{ 1-GetRandomPlace 2改变不透明度从0到1并返回(平滑过渡) } 看起来很简单,但我不能理解如何在Xcode中正确地完成它。 这是我的测试代码,但看起来没用 谢谢你的帮助,对不起,如果已经有这个问题,我找不到。

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
}

}

1 个答案:

答案 0 :(得分:1)

The problem is in t.strftime("%A") => "Tuesday" .

You are immediately creating 4 animations on the same view but only one can run at a time. What you need to do is wait until one animation is finished (fade in or fade out) before starting a new one.

Also, the AnimationBackgroundDots closure is for setting the state you want your view to animate to. It looks at how your view is at the start, runs animations, then looks at the view again and figures out how to animate between the two. In your case, the animations of the alpha starts at 0, then when UIImageView runs, the animations ends up being 0 so nothing would animate. You can't create all the steps an animation should take that way.

Want you need to do it move your view and start the fade in animation. The completion closure of fading in should start the fade out animation. The completion closure of the fading out should then start the process all over again. It could look something like this.

alpha

You called also look at using keyframe animations but this case is simple enough that theres no benefit using them.


Also as a side note. The function naming convention in swift is to start with a lowercase letter, so 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) }) }) } should be AnimationBackgroundDots.