gpt4 book ai didi

ios - 我如何向使用点击手势识别器的图像添加事件指示器

转载 作者:行者123 更新时间:2023-11-28 07:58:51 25 4
gpt4 key购买 nike

我想知道如何将事件指示器添加到使用点击手势识别器的图像,如果用户点击图像需要很长时间才能加载,就像应用程序没有响应一样我无法在代码中知道如何添加它acton,当用户点击图片时 enter image description here

这是点击手势代码

     override func viewDidLoad() {
super.viewDidLoad()

profileImage.layer.cornerRadius = profileImage.frame.size.width/2
profileImage.clipsToBounds = true
profileImage.contentMode = .scaleAspectFill
let tapGestur = UITapGestureRecognizer(target: self, action: #selector(SignupVC.selectProfileImage))
profileImage.addGestureRecognizer(tapGestur)
profileImage.isUserInteractionEnabled = true
}

@objc func selectProfileImage() {
let pickerController = UIImagePickerController()
pickerController.delegate = self
PiActivityIndicator.startAnimating()
present(pickerController, animated: true, completion: nil)
pickerController.allowsEditing = true

}

extension SignupVC: UINavigationControllerDelegate,UIImagePickerControllerDelegate {


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
selectedImage = image
profileImage.image = image
self.profileActivityIndicator.startAnimating()

}


dismiss(animated: true, completion: nil)
}
}

最佳答案

这里我使用了一个 imagePicker 和一个来自 storyBoard 的 Indicator而不是手势使用按钮 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 我使用了一个按钮,只需在您的点击手势中添加我的按钮操作代码并在点击手势中启动微调器并在选择器委托(delegate)中隐藏动画微调器

关于ios - 我如何向使用点击手势识别器的图像添加事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47215959/

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