我正在尝试制作一个小游戏。动画有一些问题。我是 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
}
最佳答案
问题出在 AnimationBackgroundDots
中。
您立即在同一个 View 上创建了 4 个动画,但一次只能运行一个。您需要做的是等到一个动画完成(淡入或淡出)后再开始一个新动画。
此外,animations
闭包用于设置您希望 View 动画的状态。它会查看 View 在开始时的情况,运行动画,然后再次查看 View 并找出如何在两者之间设置动画。在您的情况下,UIImageView
的 alpha
从 0 开始,然后当 animations
运行时,alpha
结束为 0,所以没有任何动画。您无法以这种方式创建动画应采取的所有步骤。
您需要移动 View 并开始淡入淡出动画。淡入完成关闭应该开始淡出动画。淡出的完成关闭应该重新开始整个过程。它可能看起来像这样。
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
。
关于ios - Swift UIView 出现在不同的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34163842/