从完成事件中调用swift动画?(Calling swift animation from completion event?)

编程入门 行业动态 更新时间:2024-10-27 01:22:27
从完成事件中调用swift动画?(Calling swift animation from completion event?)

我试图在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") })

I am trying to call an animation event on a FBSDKLoginButton in swift. I can tell the animation is being call, because the elements that are changing alpha values function correctly. For some reason though, the button will not move in the same call.

If I touch another button, calling the exact same function, the button then moves. Not sure why calling a function from a completion event vs. another location would affect whether or not the object actually moves?!

Under the BTN_Animate, that where the call works. After the

LocalProfile.sharedInstance.updateFromFacebook(updateFromFacebook_Complete:

call you can see the same function, and the argument being passed is assumed 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") } }

Any help here would be appreciated! Thanks

EDIT: Below code returns that it IS the main thread...

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

Work around: Try to update your buttons frame in the main queue. In swift 3.0 you can do as bellow:

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

更多推荐

本文发布于:2023-07-29 18:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1318679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动画   事件中   swift   Calling   completion

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!