Ios 如何确保在animateWithDuration()中调用completion

Ios 如何确保在animateWithDuration()中调用completion,ios,swift,Ios,Swift,我有一个在UICollectionView之间切换的功能: func toggleCollectionView(target: NSObject, targetName:String){ self.view.userInteractionEnabled = false if let tempTarget = target as? UICollectionView { //if selected item is same as active one, won't d

我有一个在UICollectionView之间切换的功能:

func toggleCollectionView(target: NSObject, targetName:String){
    self.view.userInteractionEnabled = false
    if let tempTarget = target as? UICollectionView {
        //if selected item is same as active one, won't do anything
        if(targetName != activeToolbarName){

            tempTarget.hidden = false
            tempTarget.frame.origin.y = screenSize.height
            UIView.animateWithDuration(0.4, delay: 0.0, options: .CurveEaseOut, animations: {
                if let tempActiveToolbar  = self.activeToolbar as? UICollectionView {
                    tempActiveToolbar.frame.origin.y = self.screenSize.height
                }

                tempTarget.frame.origin.y = self.screenSize.height - tempTarget.frame.height - self.selectorsContainer.frame.height
                }, completion: { finished in
                    if let tempActiveToolbar  = self.activeToolbar as? UICollectionView {
                        tempActiveToolbar.hidden = true
                        self.activeToolbar = target
                        self.activeToolbarName = targetName
                        self.view.userInteractionEnabled = true

                    }

            })
        }

    }

}
它由屏幕上的几个按钮触发,如下所示:

@IBAction func showFontsTool(sender: UIBarButtonItem) {
   toggleCollectionView(fontsCV, targetName:"fontsCV")
}
如果用户快速点击按钮,
completion
块有时不会被调用,也不会启用
self.view.userInteractionEnabled
。如何确保启动动画后始终调用
completion

更新

固定功能,工作良好:

func toggleCollectionView(target: NSObject, targetName:String){

    if let tempTarget = target as? UICollectionView {
        //if selected item is same as active one, won't do anything
        if(targetName != activeToolbarName){
            tempTarget.hidden = false
            tempTarget.frame.origin.y = screenSize.height

            if (runningAnimation == false){
                runningAnimation = true
                self.activeToolbarName = targetName
                UIView.animateWithDuration(0.4, delay: 0.0, options: .CurveEaseOut, animations: {
                    if let tempActiveToolbar  = self.activeToolbar as? UICollectionView {
                        tempActiveToolbar.frame.origin.y = self.screenSize.height
                    }

                    tempTarget.frame.origin.y = self.screenSize.height - tempTarget.frame.height - self.selectorsContainer.frame.height
                    }, completion: { finished in
                        if let tempActiveToolbar  = self.activeToolbar as? UICollectionView {
                            tempActiveToolbar.hidden = true
                            self.activeToolbar = target
                            self.runningAnimation = false                            }
                })
            }

        }

    }

}
这是一个解决办法:

创建一个布尔变量
animationTriggering
,当按下按钮时,该变量将自身设置为true。此变量可作为您检查的标志;如果为真,请不要再次执行UIView动画。您将仅在UIView动画处于false状态时执行它

UIView动画完成后,将其设置回false,以便下次再次触发

我猜问题是由多线程问题造成的。我以前也遇到过这种情况,但我真的没有信心说我知道问题出在哪里


如果我可以冒昧猜测一下,那就是UIView动画有一个内部异步线程,如果您发送请求太快,它会相互干扰。

首先,您必须声明一个全局Bool变量

var isAnimating:BOOL = False
现在在按钮操作中

@IBAction func showFontsTool(sender: UIBarButtonItem) {
   if(!isAnimating) //if animation is not happening then call the method
   {
     toggleCollectionView(fontsCV, targetName:"fontsCV")
   }
   else  //if animation is happening then return
   {
     return;
   }
}

现在,在动画方法(即
func-toggleCollectionView
)中,设置
isAnimating
变量的值,如下所示:-

func toggleCollectionView(target: NSObject, targetName:String){
    isAnimating=True   //notify animation has started, so that this method don't get fire on button click
    self.view.userInteractionEnabled = false
    if let tempTarget = target as? UICollectionView {
        //if selected item is same as active one, won't do anything
        if(targetName != activeToolbarName){

            tempTarget.hidden = false
            tempTarget.frame.origin.y = screenSize.height
            UIView.animateWithDuration(0.4, delay: 0.0, options: .CurveEaseOut, animations: {
                if let tempActiveToolbar  = self.activeToolbar as? UICollectionView {
                    tempActiveToolbar.frame.origin.y = self.screenSize.height
                }

                tempTarget.frame.origin.y = self.screenSize.height - tempTarget.frame.height - self.selectorsContainer.frame.height
                }, completion: { finished in
                    if let tempActiveToolbar  = self.activeToolbar as? UICollectionView {
                        tempActiveToolbar.hidden = true
                        self.activeToolbar = target
                        self.activeToolbarName = targetName
                        self.view.userInteractionEnabled = true
                        isAnimating=False //now set bool value  on this completion handler to false so that this method can get fired again. 

                    }

            })
        }

    }

}

忘了提了。我建议您将userInteraction enabled设置为false,前提是您确定要将其交换回true。。。你应该把它放在
ui视图的前面。设置动画
呼叫。谢谢你的回答。