关闭UIImagePickerController后的快速动画



我尝试在UIImagePickerController被关闭后将图像添加到带有动画的堆栈视图中。但是动画根本不起作用。如何确保动画在控制器关闭后发生?动画似乎受到UIImagePickerController的模具关闭动画的阻碍。

视图控制器:

import UIKit
class CustomViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    var customView: CustomView!
    var imagePicker: UIImagePickerController!
    override func viewDidLoad() {
        super.viewDidLoad()
        customView = CustomView.init(profilePic: UIImage(named: "profilePic")!)
        customView.addPictureSegment.addPictureButton.addTarget(self, action: #selector(onAddPicture), for: .touchUpInside)
        view.addSubview(customView)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    //Open imagePicker to take picture
    @objc func onAddPicture() {
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .camera
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        imagePicker.dismiss(animated: true, completion: nil)
        let image = info[UIImagePickerControllerOriginalImage] as? UIImage
        imagePicker.dismiss(animated:true, completion: nil)
        customView.addPictureSegment.addImage(image: image)
    }
}

自定义视图:

class CustomeView: UIScrollView {
    var contentView: UIView!
    var addPictureSegment: HorizontalPictureGallery!
    init(profilePic: UIImage) {
        super.init(frame:CGRect.zero)
        self.contentView = UIView()
        self.addSubview(self.contentView)
        self.setupPicSegment()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupPicSegment() {
        //Add picture segment
        addPictureSegment = HorizontalPictureGallery()
        contentView.addSubview(addPictureSegment)
    }
}

视图承载堆栈视图:

class HorizontalPictureGallery: UIScrollView {
    var addPictureButton: AddImageButton!
    var contentView: UIView!
    var horizontalImageStackView: UIStackView!
    init() {
        super.init(frame: CGRect.zero)
        contentView = UIView()
        addPictureButton = AddImageButton()
        self.addSubview(contentView)
        setupStackView()
        setupConstraints()
    }
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupStackView() {
        horizontalImageStackView = UIStackView()
        horizontalImageStackView.axis = .horizontal
        horizontalImageStackView.contentMode = .scaleToFill
        horizontalImageStackView.spacing = MyMargins.mediumMargin
        horizontalImageStackView.addArrangedSubview(addPictureButton)
        self.addSubview(horizontalImageStackView)
    }
    @objc func addImage(image: UIImage) {
        let imageView = UIImageView()
        imageView.image = image
        imageView.contentMode = .scaleToFill
        imageView.isHidden = true
        horizontalImageStackView.insertArrangedSubview(imageView, at: 0)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.widthAnchor.constraint(equalTo: horizontalImageStackView.heightAnchor).isActive = true
        UIView.animate(withDuration: 0.9){
            imageView.isHidden = false
        }
    }
    private func setupConstraints() {
        ...
    }
}

动画.isHidden不适用于UIView.animate。如果使用 animate 方法,则必须使用 false .isHidden并将alpha从 0 更改为 1。相反,请尝试转换:

UIView.transition(with: view, duration: 0.9, options: .transitionCrossDissolve, animations: {
        imageView.isHidden = false
    })

相关内容

  • 没有找到相关文章

最新更新