Page 1 of 1

Wie zeige ich eine Animation in einem anderen Controller?

Posted: 13 Jan 2025, 19:59
by Guest
Ich möchte eine Animation anzeigen, nachdem mein Ansichtscontroller mit der Bildansicht geöffnet wurde. Ich verwende dazu diesen Code, aber die Animation funktioniert in meiner App nicht:
View Controller

Code: Select all

class ViewController: UIViewController {

lazy var button: UIButton = {
let button = UIButton()
button.backgroundColor = .darkGray
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(show), for: .touchUpInside)
return button
}()

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
button.frame = CGRect(x: 500, y: 200, width: 100, height: 100)
}

@objc func show() {
let detailController = OnboardingController()
self.present(detailController, animated: true, completion: nil)
}

}
OnboardingController-Code:

Code: Select all

class OnboardingController: UIViewController {

private let imageView: UIImageView = {
let image = UIImageView()
image.image = UIImage(named: "1")
image.translatesAutoresizingMaskIntoConstraints = false
return image
}()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(imageView)

imageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: -320).isActive = true
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true

imageView.layer.removeAllAnimations()
imageView.transform = CGAffineTransformTranslate(.identity, 0, 0)

UIView.animate(withDuration: 1.0, delay: 0, animations: {
self.imageView.transform = CGAffineTransformTranslate(.identity, 320, 0)
}, completion: nil)

}
}
Wie kann dieses Problem gelöst werden?