我如何为使用点击手势识别器的图像添加活动指示器

时间:2017-11-10 05:04:18

标签: ios iphone swift uiactivityindicatorview uitapgesturerecognizer

我想知道如何为使用点按手势识别器的图像添加活动指示器,如果用户点击图片,则需要花费太长时间来加载,就像应用没有响应我无法知道在代码中如何添加此acton以及用户点击图片enter image description here

这是点击手势代码

{{1}}

1 个答案:

答案 0 :(得分:0)

这里我使用了storyBoard中的imagePicker和Indicator 而不是使用Gesture按钮Action 问题是显示活动指标直到图像选择器加载时间

使用以下代码参考

import UIKit

class ProfileViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var profileImageView: UIImageView!

    @IBOutlet weak var Spinner: UIActivityIndicatorView!

    override func viewDidLoad() {
        super.viewDidLoad()

        Spinner.isHidden = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func imageSelectAction(_ sender: Any) {
        //your tap gesture action like 
        //show and start animating spinner 
        //animate till output is not achieved 
        Spinner.isHidden = false
        self.Spinner.startAnimating()

        //Initiate a picker here 
        let picker = UIImagePickerController()
        //Delegates
        picker.delegate = self
        picker.allowsEditing = true
        //present to call imagePickerController delegate function
        present(picker, animated: true, completion: nil)
    }

    //delegate method
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        //selected image from picker
        var selectedImageFromPicker: UIImage?
        //additional checks if required 
        if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage
        {
            selectedImageFromPicker = editedImage
        }
        else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage
        {

            selectedImageFromPicker = originalImage
        }

        //here set value in your outlet profile image 
        if let selectedImage = selectedImageFromPicker
        {
            profileImageView.image = selectedImage
        }

        //Hide spinner and stop animating
        Spinner.isHidden = true
        self.Spinner.stopAnimating()
        dismiss(animated: true, completion: nil)

    }

    //if picker is cancelled in any case without selection
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        print("canceled picker")
        //if picker is cancelled so need to hide animating spinner
        Spinner.isHidden = true
        self.Spinner.stopAnimating()
        dismiss(animated: true, completion: nil)
    }
}

还可以轻松使用TapGesture我使用了一个按钮,只需在您的点按手势中添加我的按钮动作代码并以轻击手势启动微调器 并在选择器代理中隐藏动画微调器