简体   繁体   English

Xcode Swift-对UIImageView进行重复动画处理

[英]Xcode Swift - Animating UIImageView Repetitively

just quick inquiry in regards to implementing an issue I'm having with animating a UIImageView. 只是快速询问有关实现UIImageView动画的问题。 I successfully implemented animating an image to slide off screen; 我成功实现了动画图像滑出屏幕的功能; but i want it to reappear when it exists the view to simulate a side-scroller game animation. 但我希望当它存在以模拟侧滚动游戏动画的视图时重新出现。

Something like this: 像这样: 在此处输入图片说明

I've tried implementing a completion handler but struggled understanding the logic of how to implement it, so I removed my attempts; 我曾经尝试实现一个完成处理程序,但是却难以理解如何实现它的逻辑,因此我取消了尝试。 so my code is left as follows: 所以我的代码如下:

let oldCenter = background.center
        let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y)

        UIView.animate(withDuration: 2, delay: 0, options: .curveLinear, animations: {
            self.background.center = newCenter
        }) { (success: Bool) in
            print("Done moving image")
        }

An example or pointers on how to achieved my desired animation would be appreciated! 一个例子或有关如何实现我想要的动画的指针将不胜感激!

let oldCenter = view.center
    let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y)
    UIView.animate(withDuration: 2.0, delay: 0.0, options: 
    [.curveLinear, .repeat], animations: {
      view.center = newCenter
    }, completion: nil)

If you want to repeat the animation, there is no need to implement the completion . 如果要重复动画,则无需实施completion animate(withDuration:delay:options:animations:completion:) has options parameter ( UIViewAnimationOptions ), which is: animate(withDuration:delay:options:animations:completion :)具有options参数( UIViewAnimationOptions ),它是:

A mask of options indicating how you want to perform the animations. 选项掩码,指示您要如何执行动画。 For a list of valid constants, see UIViewAnimationOptions. 有关有效常量的列表,请参见UIViewAnimationOptions。

One of the constants for the UIViewAnimationOptions is repeat : UIViewAnimationOptions的常量之一是repeat

Repeat the animation indefinitely. 无限重复动画。

So, what you should do: 因此,您应该做什么:

UIView.animateKeyframes(withDuration: 2.0, delay: 0.0, options: [.curveLinear, .repeat], animations: {
    self.background.center = newCenter
}, completion: nil)

Again, implementing completion is not required for repeating the animation, implementing it is up to your case. 同样,重复动画不需要实现completion ,具体取决于您的情况。

You can try UIView UIView repeat option 您可以尝试UIView UIView重复选项

    let oldCenter = background.center
    let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y)

    UIView.animate(withDuration: 2, delay: 0, options: [.curveLinear, repeat], animations: {
        self.background.center = newCenter
    }) { (success: Bool) in
        print("Done moving image")
        if(success)
        {
          self.background.center = oldCenter
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM