[英]Activity indicator starts before function is called
I have a button that calls this IBAction function. 我有一个调用此IBAction函数的按钮。 The activity indicator starts as soon as the app runs instead of when the button is tapped, disappears as expected when the function completes its work then never appears again. 活动指示器会在应用程序运行时立即启动,而不是在点击按钮时启动,在功能完成其工作后按预期消失,然后不再显示。 This seems like a really simple interface thing but I'm stumped. 这似乎是一个非常简单的界面,但是我很困惑。 Any ideas? 有任何想法吗?
@IBAction func saveToCamera() {
// Start the spinner
activityIndicator.startAnimating()
//Create the UIImage
UIGraphicsBeginImageContext(canvas.frame.size)
canvas.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
activityIndicator.stopAnimating()
if error != nil {
println(error) }
}
It sounds like you have the isAnimating
property on the activity view set to true in your storyboard. 听起来好像您在isAnimating
中将活动视图上的isAnimating
属性设置为true。 make sure it's false, and that the hidesWhenStopped
property is true. 确保它为假,并且hidesWhenStopped
属性为true。
Then your code won't work as posted. 然后,您的代码将无法正常发布。 If you want the spinner to stop animating when the call to UIImageWriteToSavedPhotosAlbum
is complete, you need to provide a completionTarget
and completionSelector
. 如果你想微调停止动画时调用UIImageWriteToSavedPhotosAlbum
完成后,您需要提供一个completionTarget
和completionSelector
。 Something like this: 像这样:
(Edited to use correct method signature for completion method) (编辑为完成方法使用正确的方法签名)
UIImageWriteToSavedPhotosAlbum(
image,
self,
"image:didFinishSavingWithError:contextInfo:",
nil)
And a method to be called once the write is complete: 写入完成后要调用的方法:
func image(image: UIImage,
didFinishSavingWithError
error: NSErrorPointer,
contextInfo: UnsafePointer<Void>)
{
activityIndicator.stopAnimating()
if error != nil
{
println(error)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.