@IBOutlet weak var imageView: UIImageView!
override func viewWillAppear(animated: Bool) {
self.imageView.center.y -= view.bounds.height
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
self.imageView.center.y += self.view.bounds.height
}, completion: nil)
}
This code does successfully move the image view across the screen, but I'm not sure how to make it stop where I want it to. It just slides off the screen. The goal is to move the image from point A to point B. If there is another easier way to do this I would be happy to know. Thanks!
Add this in viewWillAppear,you will get your animation,you problem is autoLayout
override func viewWillAppear(animated: Bool) {
self.imageview.setTranslatesAutoresizingMaskIntoConstraints(true)
self.imageview.center.y -= self.view.bounds.height
}
When you move a view frame,it is better use animate antoLayout Constraint
Drag the red Constraint into outlet then
Example Code
class ViewController: UIViewController{
@IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var yConstraint: NSLayoutConstraint!
override func viewWillAppear(animated: Bool) {
yConstraint.constant -= self.view.bounds.height;
self.view.updateConstraintsIfNeeded()
self.view.layoutIfNeeded()
}
override func viewDidAppear(animated: Bool) {
yConstraint.constant += self.view.bounds.height;
UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
}
override func viewDidAppear(animated: Bool) {
var destinationY:CGFloat = self.view.bounds.height / 2 // Your destination Y
UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
self.imageView.center.y = destinationY
}, completion: nil)
}
Just moving it too far, that'll put the center in the middle of the screen.
For this line: self.imageView.center.y += self.view.bounds.height
Maybe make it move less? self.imageView.center.y += self.view.bounds.height - 200
Your implementation is fine, you just have to understand the amount you are changing y
by. self.view.bounds.height
refers to the height of the view controllers view. That's probably pretty big, in fact, likely the entire height of the screen you're looking at. So you need to decide what point you want to move it to and do some math. Say you want to stop it when it hits the top edge of the screen. You might do something very simple like:
self.imageView.center.y += (self.view.frame.origin.y - self.imageView.frame.origin.y)
I did not test this, please implement in the way that works best for your situation.
override func
viewDidAppear(animated: Bool) {
super.viewDidAppear( animated )
let orgY = self.imageView.center.y
self.imageView.center.y -= view.bounds.height
UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
self.imageView.center.y = orgY
}, completion: nil)
}
Save original point and go!