PDA

View Full Version : Snap a UIImageView to particular area of iPad screen, with springback animation.




Grannyville7989
Feb 27, 2012, 09:30 AM
I'm new to iOS programming and I'm trying to get a particular task done but I'm having difficulty finding the information for what I'm looking for so I thought I'll give it a shot here.

I've got a view with four UIImageViews in a row at the top screen, which can be dragged around with your finger. I want to know how I can make it so that once an image has been dragged into a particular area on screen, it will "snap" into that area. And if the image is not dragged into that particular area on the screen, it springs back to it's original starting point, with an animation of it springing back there.

I can providing more information on request if needed.



seepel
Feb 28, 2012, 12:59 AM
Take a look at the animation methods in the UIView documentation. You could also try a google search like "Animating UIView" and you might find some decent examples.

In short you can nearly automatically animate the movement of a view when you change the view's frame.

jnoxx
Feb 28, 2012, 02:31 AM
Use Animation blocks, by setting the frame/center of it, and it should take care of the smoothness on OS Level.
And the check of being in that area is checking something like
CGRectIntersects (or something, top of my head).
on the drag function.

Greetz, Noxx

Grannyville7989
Feb 28, 2012, 03:00 AM
Take a look at the animation methods in the UIView documentation. You could also try a google search like "Animating UIView" and you might find some decent examples.

In short you can nearly automatically animate the movement of a view when you change the view's frame.

Thank you. I exactly sure what I should punch into Google to look up what I wanted. I'll give it a gander later on today.

samdev
Feb 28, 2012, 10:14 PM
I'm new to iOS programming and I'm trying to get a particular task done but I'm having difficulty finding the information for what I'm looking for so I thought I'll give it a shot here.

I've got a view with four UIImageViews in a row at the top screen, which can be dragged around with your finger. I want to know how I can make it so that once an image has been dragged into a particular area on screen, it will "snap" into that area. And if the image is not dragged into that particular area on the screen, it springs back to it's original starting point, with an animation of it springing back there.

I can providing more information on request if needed.

UIImageView doesn't snap positions, that's something you have to do in code.

I would just check the distance between the empty slot and the image, and if it's within
a specific range, animate it to the slot. Otherwise animate it back to its original position.

Basic animation:

[UIView beginAnimations: nil context: NULL];
myImageView.frame = CGRectMake(50,50, 100,100); // move to here
[UIView commitAnimations];


You can set additional options, such as delegate, duration, curve parameters, etc... if needed.

jnoxx
Feb 29, 2012, 02:32 AM
To Samdev: You are using Pre iOS4 Transitions, which will work fine, but I encourage to use the UIAnimation code blocks ^_-