从完成事件中调用快速动画?

时间:2017-01-31 01:20:41

标签: ios swift animation

我试图在swift中调用FBSDKLoginButton上的动画事件。我可以告诉动画正在调用,因为正在改变alpha值的元素正常运行。但由于某种原因,该按钮不会在同一个呼叫中移动。

如果我触摸另一个按钮,调用完全相同的功能,则按钮移动。不确定为什么从完成事件到另一个位置调用函数会影响对象是否实际移动?!

在BTN_Animate下,呼叫工作的地方。

之后
LocalProfile.sharedInstance.updateFromFacebook(updateFromFacebook_Complete:

调用你可以看到相同的函数,并且传递的参数假定为true。

@IBAction func BTN_Animate(_ sender: Any) {

    animateScreen(loggedIn: true)  //Call that works
}

public func loginButton(_ BTN_facebookLogin: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    print("> Attempting Login from Main View Controller")

    if ((error) != nil)
    {
        print("Error: ")
        // Process error
        print(error)
    }
    else if result.isCancelled {
        // Handle cancellations
        print("Request cancelled")
        print(result)
    }
    else {
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing


        LocalProfile.sharedInstance.updateFromFacebook(updateFromFacebook_Complete: {

            self.LBL_Name.text = LocalProfile.sharedInstance.firstName

            self.IMG_ProfilePicture.image = LocalProfile.sharedInstance.profilePicture

            LocalProfile.sharedInstance.printAllData()

            self.animateScreen(loggedIn: LocalProfile.sharedInstance.loggedIn)  //Call that doesn't work

        })




    }

}

public func animateScreen(loggedIn: Bool) {

    if(loggedIn) {

        //Animating screen objects
        print("> Animating screen objects")

        UIView.animate(withDuration: 0.7, delay: 1.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.BTN_facebookLogin.frame = CGRect(x: self.BTN_facebookLogin.frame.origin.x, y: (self.view.bounds.height - self.BTN_facebookLogin.frame.size.height) - 50, width: self.BTN_facebookLogin.frame.size.width, height: self.BTN_facebookLogin.frame.size.height)

            self.IMG_ProfilePicture.alpha = 1
            self.LBL_Name.alpha = 1

            self.view.layoutIfNeeded()
        }, completion: { finished in
            print("> Done animating screen objects")
        })



    }
    else {

        //Not animating
        print("> Not animating screen objects")

    }


}

任何帮助都将不胜感激!感谢

编辑:下面的代码返回它是主线程......

LocalProfile.sharedInstance.updateFromFacebook(updateFromFacebook_Complete: {

            self.LBL_Name.text = LocalProfile.sharedInstance.firstName

            self.IMG_ProfilePicture.image = LocalProfile.sharedInstance.profilePicture

            LocalProfile.sharedInstance.printAllData()

            self.animateScreen(loggedIn: LocalProfile.sharedInstance.loggedIn)

            let notString = Thread.isMainThread ? "" : "not "
            print("This is " + notString + "the main thread")

        })

2 个答案:

答案 0 :(得分:1)

解决方法: 尝试更新主队列中的按钮框架。在swift 3.0中,您可以执行以下操作:

DispatchQueue.main.async
{
    self.BTN_facebookLogin.frame = CGRect(x: self.BTN_facebookLogin.frame.origin.x, y: (self.view.bounds.height - self.BTN_facebookLogin.frame.size.height) - 50, width: self.BTN_facebookLogin.frame.size.width, height: self.BTN_facebookLogin.frame.size.height)
    self.IMG_ProfilePicture.alpha = 1
    self.LBL_Name.alpha = 1
    self.view.layoutIfNeeded()
}

答案 1 :(得分:1)

正如其他人所说,你必须从主线程进行UI调用。我不熟悉Facebook的API,所以我不知道你使用的完成块是否在主线程上被调用。您可以使用以下代码进行检查:

let notString = Thread.isMainThread ? "" : "not "
print("This is " + notString + "the main thread")

(那是Swift 3代码)

将它放在完成块的顶部,看看它显示在控制台上的内容。

我敢打赌,完成块不会在主线程上运行。

从后台线程执行UI调用的最常见效果是没有任何反应,或者需要很长时间才能生效。然而,它也可能导致其他奇怪的效果,甚至崩溃,所以重要的是不要这样做。

您可能也遇到自动布局干扰帧设置的问题,但是根据您描述的症状(不是从完成块工作但如果直接调用它就可以工作),这听起来更像是一个线程问题。 / p>

每当你处理完成块时,你应该弄清楚是否可以从后台线程调用完成块。 Apple的URLSession类是一个类的示例,它在后台线程上调用它的完成块。文档应该告诉你是否在后台线程上调用了一个完成块,但我上面发布的测试代码是一个很好的确保方法。

相反,UIView animateWithDuarion(animations:completion:)系列方法在主线程上调用它们的完成块。