解雇UIImagePickerController后的Swift动画

时间:2018-03-13 18:33:26

标签: swift animation uistackview

在UIImagePickerController被解除后,我尝试使用动画将图像添加到stackview。但动画根本不起作用。我怎样才能确定动画是在控制器被解除后发生的? 似乎动画受到UIImagePickerController的消除动画的阻碍。

ViewController:

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 个答案:

答案 0 :(得分:0)

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

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