Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从完成事件调用swift动画?_Ios_Swift_Animation - Fatal编程技术网

Ios 从完成事件调用swift动画?

Ios 从完成事件调用swift动画?,ios,swift,animation,Ios,Swift,Animation,我正在尝试调用swift中FBSDKLoginButton上的动画事件。我可以看出动画正在被调用,因为改变alpha值的元素功能正常。但由于某些原因,按钮不会在同一通话中移动 如果我触摸另一个按钮,调用完全相同的功能,按钮就会移动。不确定为什么从完成事件调用函数与从其他位置调用函数会影响对象是否实际移动 在BTN_动画下,调用工作的位置。之后 LocalProfile.sharedInstance.updateFromFacebook(updateFromFacebook_Complete:

我正在尝试调用swift中FBSDKLoginButton上的动画事件。我可以看出动画正在被调用,因为改变alpha值的元素功能正常。但由于某些原因,按钮不会在同一通话中移动

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

在BTN_动画下,调用工作的位置。之后

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

        })
解决办法: 尝试更新主队列中的按钮框架。在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()
}

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

let notString = Thread.isMainThread ? "" : "not "
print("This is " + notString + "the main thread")
(这是Swift 3代码)

把它放在完成块的顶部,看看它在控制台上显示了什么

我打赌完成块没有在主线程上运行

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

您可能还遇到自动布局干扰帧设置的问题,但根据您描述的症状(不是从完成块工作,而是直接调用完成块工作),这听起来更像是线程问题

任何时候处理完成块时,都应该弄清楚是否可以从后台线程调用完成块。苹果的
URLSession
类就是一个在后台线程上调用其完成块的类的例子。文档应该告诉您是否在后台线程上调用了完成块,但是我上面发布的测试代码是一个很好的确定方法


相反,
UIView
方法家族在主线程上调用它们的完成块。

提示-始终在主队列上进行UI更新。感谢您的回复!只是为了提供信息,我按照你说的做了,并且得到了它是主线程。编辑下面的代码:添加为编辑,因为注释格式围绕我想从completionHandler制作动画的任何内容添加DispatchQueue.main.async{},修复了该问题!谢谢