关闭 UIImagePickerController 后的 Swift 动画

标签 swift animation uistackview

在 UIImagePickerController 被解除后,我尝试将图像添加到带有动画的 stackview 中。但动画根本不起作用。我如何确保动画在 Controller 关闭后发生? 看来动画受到了 UIImagePickerController 的死掉动画的阻碍。

View Controller :

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)
    }
}

自定义 View :

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

最佳答案

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

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

关于关闭 UIImagePickerController 后的 Swift 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49263467/

相关文章:

ios - iOS 中的多选

ios - 当tableView高度为真实高度时

swift - 中心对齐 subview

swift - 在导航 Controller 中测试 topviewcontroller

javascript - RequestAnimationFrame定期加快/降低速度

ios - UIStackView 在 Storyboard 中添加重叠 View

java - JButton悬停效果动画。通过鼠标监听器更改不透明度

javascript - 如何使用 javascript 显示 PNG 图像的动画图像? [ 如 Gmail ]

ios - UIStackView 可以被约束吗?

ios - 扩展 UITableView 以显示 Stack View 中的所有单元格?