Reputation: 439
I was hoping someone could help.
I have two rectangles which move across the screen. The first constraints for the (rectangles) are activated in my setupViews function which is called in my viewDidLoad.
In my viewDidLayoutSubviews I have an animation function which is called moving the two rectangles.
When I click a button however I would like the rectangles to return to their initial position and begin the animation from the beginning.
How could I successfully do this?
I initially did not have my animation being called in my viewDidLayoutSubviews, just in another function (which was being called in my viewDidAppear(_ animated: Bool)), however, upon reading it seems as if calling the animation in the viewDidLayoutSubviews might be the best course of action.
I can successfully stop the animation, but getting it to restart again from its initial starting point is proving to be troublesome.
I have tried creating the constraints again in the buttons sender function. But this doesn’t seem to do anything, as I’m not sure you can change an elements constraints without reloading the viewDidLoad again.
Below is my code………
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
func setupViews() {
self.view.addSubview(backgroundImageView)
self.backgroundImageView.addSubview(contentView)
self.backgroundImageView.addSubview(slideDownTintedWhiteView)
self.backgroundImageView.addSubview(restartSlidingAnimation)
self.contentView.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
self.slideDownTintedWhiteView.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.leading.equalToSuperview()
make.trailing.equalToSuperview()
make.bottom.equalTo(backgroundImageView.snp.top)
}
self.restartSlidingAnimation.snp.makeConstraints { (make) in
make.centerX.equalToSuperview()
make..centerY.equalToSuperview()
make.width.equalTo(10)
make.height.equalTo(10)
}
self.restartSlidingAnimation.addTarget(self, action: #selector(restartAnimationFunction(_:)), for: .touchUpInside)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
UIView.animate(withDuration: 10, delay: 0, animations: {
//Red View:
self.contentView.frame = CGRect(x: self.contentView.frame.origin.x, y: self.contentView.frame.height, width: self.contentView.frame.width, height: self.contentView.frame.height)
//White View:
self.slideDownTintedWhiteView.frame = CGRect(x: self.contentView.frame.origin.x, y: 0, width: self.contentView.frame.width, height: self.contentView.frame.height)
}, completion: nil)
}
restartAnimationFunction() {
// Here is obviously where I need to somehow re-establish the constraints of the rectangles back to their original location or somehow start the animation from the beginning.
}
func stopContentViewAnimation() {
// This is where I am able to stop the animation (called from another button)
pausedTime = self.contentView.layer.convertTime(CACurrentMediaTime(), from: nil)
self.contentView.layer.speed = 0.0
self.contentView.layer.timeOffset = pausedTime
pausedTime = self.slideDownTintedWhiteView.layer.convertTime(CACurrentMediaTime(), from: nil)
self.slideDownTintedWhiteView.layer.speed = 0.0
self.slideDownTintedWhiteView.layer.timeOffset = pausedTime
}
Thanks for any help you can provide.
Upvotes: 0
Views: 620
Reputation: 100503
1- First the animation should not be inside viewDidLayoutSubviews
as it's being called many times , best place for it is viewDidAppear
2- You don't set any constraints to backgroundImageView
3- To animate a child remove it's constraints from itself and from it's parent
self.backgroundImageView.removeConstraints(self.backgroundImageView.constraints)
self.contentView.removeConstraints(self.contentView.constraints)
self.slideDownTintedWhiteView.removeConstraints(self.slideDownTintedWhiteView.constraints)
self.restartSlidingAnimation.removeConstraints(self.restartSlidingAnimation.constraints)
4- Re construct the other case constraints and when ready to animate put
self.view.layoutIfNeeded()
inside the animation block
OFF course I don't recommend removing all constraints and constructing them again , but you can have a reference to the constraints you want to change and alter their constants , but for a beginner this may be good to go
Upvotes: 2