Как анимировать UIView внутри заголовка UITableView в Swift? - PullRequest
0 голосов
/ 01 мая 2020

У меня есть stati c табличное представление с пользовательским представлением в качестве заголовка табличного представления. Вот код:

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 0:
            let headerView = UIView()
            let imageView = UIImageView()
            let label = UILabel()

            imageView.contentMode = .scaleAspectFit
            imageView.tintColor = .dirt1
            imageView.image = IconUtil.systemIcon(of: .dashboard, weight: .regular).withRenderingMode(.alwaysTemplate)
            imageView.layer.add(bounceAnimation, forKey: nil)


            label.numberOfLines = 0
            label.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
            label.lineBreakMode = .byWordWrapping
            label.textColor = UIColor.dirt1.withAlphaComponent(0.5)
            label.font = .preferredFont(forTextStyle: .caption1)


            headerView.addSubview(imageView)
            headerView.addSubview(label)

            imageView.translatesAutoresizingMaskIntoConstraints = false
            label.translatesAutoresizingMaskIntoConstraints = false

            NSLayoutConstraint.activate([
                imageView.centerXAnchor.constraint(equalTo: headerView.centerXAnchor),
                imageView.topAnchor.constraint(equalTo: headerView.topAnchor, constant: 20),
                imageView.heightAnchor.constraint(equalToConstant: 60),
                imageView.widthAnchor.constraint(equalToConstant: 60),

                label.centerXAnchor.constraint(equalTo: headerView.centerXAnchor, constant: 0),
                label.centerYAnchor.constraint(equalTo: headerView.centerYAnchor, constant: 40),
                label.widthAnchor.constraint(equalTo: headerView.widthAnchor, multiplier: 0.9)
            ])
            return headerView

        default: return nil
        }
    }

Обратите внимание, что я добавил анимацию слоя в imageView для его анимации. Вот код анимации:

    private var bounceAnimation: CAKeyframeAnimation = {
        let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
        bounceAnimation.values = [1.0, 0.9, 1.0]
        bounceAnimation.duration = TimeInterval(0.125)
        bounceAnimation.calculationMode = .cubic
        return bounceAnimation
    }()

Но когда я запускаю приложение и go к табличному виду V C, анимация не запускается. Любая помощь, как это исправить?

Вот заголовок View, если кому-то интересно. image1

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...