gpt4 book ai didi

ios - 在等待 Alamofire 请求完成时使用弹出动画对 UIView 进行动画处理

转载 作者:行者123 更新时间:2023-11-30 13:54:13 29 4
gpt4 key购买 nike

我正在尝试使用 Facebook 的 POP 框架为自定义按钮设置动画。在使用 Alamofire 完成请求之前,动画不会发生。

我想做的是发出请求并在等待过程中为按钮设置动画。

下面的代码看起来可以工作,但它在动画完成后执行请求。

 func animate(tappedView:UIView){

let rotation = POPBasicAnimation(propertyNamed: kPOPLayerRotationX)
rotation.duration = 2.0
rotation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
tappedView.layer.anchorPoint = CGPointMake(0.5, 0.5)
rotation.toValue = 3*M_PI


let scaleUp = POPBasicAnimation(propertyNamed: kPOPLayerSize)
scaleUp.duration = 0.5
scaleUp.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
tappedView.layer.anchorPoint = CGPointMake(0.5, 0.5)
let size = CGSizeMake(tappedView.frame.size.width ,tappedView.frame.size.height)
let scaledSize = CGSizeMake(size.width * 1.5, size.height * 1.5)
scaleUp.toValue = NSValue(CGSize: scaledSize)


let scaleDown = POPSpringAnimation(propertyNamed: kPOPLayerSize)
scaleDown.beginTime = CACurrentMediaTime() + 0.5
scaleDown.toValue = NSValue(CGSize: size)
scaleDown.springSpeed = 4
scaleDown.springBounciness = 8

scaleDown.completionBlock = { (anim:POPAnimation!, finished:Bool) -> Void in

let requestURL = self.prepare4SQrequest(withLocation: self.currentLocation)
let request = Alamofire.request(.GET, requestURL)
request.responseJSON{ response in
HANDLE REQUEST RESPONSE LOGIC
}


tappedView.layer.pop_addAnimation(rotation, forKey: "popRotationX")
tappedView.layer.pop_addAnimation(scaleUp, forKey: "popScaleUp")
tappedView.layer.pop_addAnimation(scaleDown, forKey: "popScaleD")

}

我尝试使用 Alamofire 的 request.progress 但没有成功。 在此版本中,动画在请求逻辑完成时开始。

func animate(tappedView:UIView){

let rotation = POPBasicAnimation(propertyNamed: kPOPLayerRotationX)
rotation.duration = 2.0
rotation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
tappedView.layer.anchorPoint = CGPointMake(0.5, 0.5)
rotation.toValue = 3*M_PI


let scaleUp = POPBasicAnimation(propertyNamed: kPOPLayerSize)
scaleUp.duration = 0.5
scaleUp.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
tappedView.layer.anchorPoint = CGPointMake(0.5, 0.5)
let size = CGSizeMake(tappedView.frame.size.width ,tappedView.frame.size.height)
let scaledSize = CGSizeMake(size.width * 1.5, size.height * 1.5)
scaleUp.toValue = NSValue(CGSize: scaledSize)


let scaleDown = POPSpringAnimation(propertyNamed: kPOPLayerSize)
scaleDown.beginTime = CACurrentMediaTime() + 0.5
scaleDown.toValue = NSValue(CGSize: size)
scaleDown.springSpeed = 4
scaleDown.springBounciness = 8


tappedView.layer.pop_addAnimation(rotation, forKey: "popRotationX")
tappedView.layer.pop_addAnimation(scaleUp, forKey: "popScaleUp")
tappedView.layer.pop_addAnimation(scaleDown, forKey: "popScaleD")

}


func buttonTapped(gesture:UIGestureRecognizer){

let tappedView:UIView = gesture.view!
switch (tappedView.tag)
{
case 1:

let requestURL = prepare4SQrequest(withLocation: currentLocation)
let request = Alamofire.request(.GET, requestURL)
request.progress({bytesRead, totalBytesRead, totalBytesExpectedToRead in
dispatch_async(dispatch_get_main_queue()) {
print("Total bytes to read : \(totalBytesExpectedToRead)")
print("Total bytes read \(totalBytesRead)")
print("Bytes read \(bytesRead)")
self.animate(tappedView)
}
} )
request.responseJSON{ response in

HANDLE REQUEST RESPONSE LOGIC

}

break
}

如有任何帮助,我们将不胜感激。

谢谢

最佳答案

您的buttonTapped函数应如下所示

func buttonTapped(gesture:UIGestureRecognizer) {
// 1 do your setup and switch case
// 2 first ask alamofire to send request
Alamofire.request(.GET, requestURL).
responseJSON {
response in
// handle response here
stopAnimation()
}
// 3 call your animation
animate(tappedView)
}

现在,如果您在 animate(tappedView) 和处理 json 响应的代码上放置断点,您将看到调试器将首先在 animate 处停止,然后一段时间后将在响应代码处停止。 Alamofire 内置了多线程逻辑,因此您要求它从网络获取某些内容,然后它会在准备就绪时执行您在闭包中传递给 .response 的代码。

希望有帮助!

关于ios - 在等待 Alamofire 请求完成时使用弹出动画对 UIView 进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33855128/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com