2017-05-01 82 views
-1

我想我的viewController中的imageView“脉动”,这意味着变大,然后再变小,重复,直到“开始”按钮被按下。问题是,每次我在viewDidLoad()中使用repeat-while循环调用该函数时,无论出于何种原因它都有一个断点。重复while循环不起作用 - 为什么?

这是()我viewDidLoad中的一部分:

repeat { 
    pulsate() 
} while hasStarted == false 

当我按下启动按钮,hasStarted成为真正的,它应该停止跳动。但它甚至不启动该功能,它立即调用一个错误。问题在哪里?

这里是我的搏动() - 函数

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) 
     } 
    } 
} 

感谢您的帮助!祝你有美好的一天!

+2

有什么错误? –

+1

您正在阻止主线程。在程序控制返回到主事件循环之前,没有按钮(或任何其他)的UI操作方法将被执行。 –

回答

3

你可以使用UIView.animate的内置选项来真正简化你想要做的事情,避免锁定线程。

试试这个:

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) 

你的ImageView现在应该在一个完整的“脉动”循环,而无需任何额外的代码。当你准备停止动画,只要致电:

imageView.layer.removeAllAnimations()