我正在尝试使用Facebooks 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
}
任何帮助都会被高估。
谢谢
答案 0 :(得分:0)
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响应的代码,你会看到你的调试器将首先停止在动画处,然后一段时间后会停在响应代码处。 Alamofire内置了多线程逻辑,所以你要求它从网络中获取一些东西,然后它会在你准备就绪的时候执行你在闭包中传递给.response的代码。
希望有所帮助!