How to animate an object vertically with touch like Spotify's music player does when tapping the song,

958 views Asked by At

Im using Xcode, and when i tap/drag on my imageView at the bottom of the screen, I want it to be brought up and reveal all of it on screen similiar to the way Spotify does it when you click the banner at the bottom. Any ideas?

2

There are 2 answers

1
LinusGeffarth On BEST ANSWER

I recommend using spring animations if you want a nice animation like this:

func buttonTapped(sender: UIButton!) { //or in an IBAction
    let duration: NSTimeInterval = 0.75
    let damping: CGFloat = 1
    let velocity: CGFloat = 0.5

    UIView.animateWithDuration(duration, delay: 0.5, usingSpringWithDamping: damping, initialSpringVelocity: velocity, options: .CurveLinear, animations: {
        self.myView.center.y = self.view.frame.height/2
        }, completion: nil)
}

Sorry for Swift code, if you're not able to translate it then let me know :)

Edit
For Objective-C the code would look something like this:

[UIView animateWithDuration:0.75, delay:0, usingSpringWithDamping:1, initialSpringVelocity:0.5, options:UIViewAnimationOptionsCurveLinear, animations:^{
    //Animations
  } completion:^(BOOL finished) {
    //Completion Block
}];
0
Lehman2020 On

I actually figured it out. I made a button and included this code to trigger the animation:

self.moveX.constant = 200;
[UIView animateWithDuration:2.0f animations:^{
    imageView.frame = CGRectMake(0.0f, 200.0f, imageView.frame.size.width,imageView.frame.size.height);
}];