简体   繁体   English

重复while循环不起作用-为什么?

[英]Repeat while loop doesn't work - why?

I want an imageView in my viewController to "pulsate", that means become bigger and then tinier again and repeat that until the "start"-button was pressed. 我希望我的viewController中的imageView发生“脉动”,这意味着先变大然后再变细,然后重复该过程,直到按下“开始”按钮为止。 The problem is, that every time I call that function in the viewDidLoad() with a repeat-while-loop, it has got a breakpoint for whatever reason. 问题是,每当我使用循环重复循环在viewDidLoad()中调用该函数时,无论出于何种原因,它都会有一个断点。

That's the part of my viewDidLoad(): 那是我的viewDidLoad()的一部分:

repeat {
   pulsate()
} while hasStarted == false

When I press the start button, hasStarted becomes true and it should stop pulsating. 当我按下开始按钮时,hasStarted变为true,它应该停止跳动。 But it doesn't even start the function, it immediately calls an error. 但是它甚至没有启动函数,而是立即调用错误。 Where's the problem? 哪里出问题了?

Here's my pulsate()-function 这是我的pulsate()函数

func pulsate()
{
    frameOR = imageView.frame
    frameNext = CGRect(x: imageView.frame.midX - 35, y: imageView.frame.midY - 35, width: 80, height: 80)
    let frameVDL = CGRect(x: imageView.frame.midX - 150, y: imageView.frame.midY - 150, width: 300, height: 300)

    if start == false
    {
        UIView.animate(withDuration: 2, animations: {
            self.imageView.frame = frameVDL
        }) { (finished) in
            UIView.animate(withDuration: 2, animations: {
                self.imageView.frame = self.frameOR
            }, completion: nil)
        }
    }
}

Thank your for your help! 感谢您的帮助! Have a great day! 祝你有美好的一天!

You can use UIView.animate 's built-in options to really simplify what you're trying to do, and avoid locking up the thread. 您可以使用UIView.animate的内置选项来真正简化您要尝试执行的操作, 避免锁定线程。

Give this a try: 试试看:

let frameOR = imageView.frame
let frameVDL = CGRect(x: imageView.frame.midX - 150, y: imageView.frame.midY - 150, width: 300, height: 300)

UIView.animate(withDuration: 2.0,
               delay: 0.0,
               options: [UIViewAnimationOptions.autoreverse, UIViewAnimationOptions.repeat],
               animations: {
                imageView.frame = frameVDL
                },
               completion: nil)

Your imageView should now be in a full "pulsing" loop, without needing any additional code. 现在,您的imageView应该处于完整的“脉冲”循环中,而无需任何其他代码。 When you're ready to stop the animation, just call: 准备停止动画时,只需调用:

imageView.layer.removeAllAnimations()

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

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