用户按下徽标后,我正在运行动画。这是动画:
func rightRotateView(targetView: UIView, duration: Double = 5) {
UIView.animate(withDuration: duration, delay: 0.0, options: [.repeat, .curveLinear] , animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat.pi * 5)
}) { finished in
// self.rightRotateView(targetView: targetView)
}
}
长按 3 秒后(此时动画仍应运行),我向用户显示消息控制器:
if MFMessageComposeViewController.canSendText() == true {
print(self.urgentNumber)
let recipients:[String] = ["(self.urgentNumber as! String)"]
self.messageController.messageComposeDelegate = self as? MFMessageComposeViewControllerDelegate
self.messageController.recipients = recipients
self.messageController.body = "Hey,nmy longitude: (self.userLocation.coordinate.longitude) nmy latitude: (self.userLocation.coordinate.latitude)"
self.present(self.messageController, animated: true, completion: nil)
} else {
//handle text messaging not available
}
当我按下消息控件中的取消按钮时,我返回到动画页面,但动画停止工作。我试图在现在之后重新运行动画,并在
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
}
但它没有奏效。
我发现了问题,只需在完成中重新运行相同的动画,(您可以使用CABasicAnimation或简单地使用UIView.animate):
func rightRotateView(targetView: UIView, duration: Double = 5) {
UIView.animate(withDuration: duration, delay: 0.0, options: [.repeat, .curveLinear] , animations: {
targetView.transform = targetView.transform.rotated(by: CGFloat.pi * 5)
}) { finished in
let anim = CABasicAnimation(keyPath:"transform.rotation")
anim.fromValue = 0.000
anim.toValue = 360.0
anim.speed = 0.001
anim.repeatCount = .infinity
targetView.layer.add(anim, forKey: "transform.rotation")
}
}
用于
此CABasicAnimation(keyPath: "transform.rotation")