关闭 UIImagePickerController 后的 Swift 动画

Posted

技术标签:

【中文标题】关闭 UIImagePickerController 后的 Swift 动画【英文标题】:Swift animation after dismissing UIImagePickerController 【发布时间】:2018-03-13 18:33:26 【问题描述】:

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

视图控制器:

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)
    

View携带stackview:

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() 
        ...
    

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

那行不通。我设置了:horizo​​ntalImageStackView,因为这是应该动画的 imageView 的容器。那仍然没有显示任何动画。 这是朝着正确方向迈出的一步,因为除非您制作 Alpha 动画,否则您需要过渡。您是否在调试器中设置了断点以确保它在主线程上被调用? 它与 UIImapgePickerController 的解除在同一个线程上运行。应该是这样的吗? 我希望它们位于主线程上,应该称为“线程 1”。对吗? 是的,就是这样。我还使用 .isHidden 独立测试了动画并且它有效(如这里:medium.com/@nrewik/easy-animation-with-uistackview-8878b2856ae2)。但是结合 imagePicker.dismiss(animated:true, completion: nil) 它不起作用。

以上是关于关闭 UIImagePickerController 后的 Swift 动画的主要内容,如果未能解决你的问题,请参考以下文章

ViewController 生命周期 UINavigationController

如何从 webview 使用 UIImagePickerController

iPhone OS 3.0 中的 UIImagePickerController

iOS开发——打开手机相册,获取图片

如何使用 iphone 中的核心数据将图像存储到 sqlite? [复制]

JFrame关闭方法的问题