Snap a UIImageView to particular area of iPad screen, with springback animation.

Discussion in 'iOS Programming' started by Grannyville7989, Feb 27, 2012.

  1. macrumors 6502a

    Grannyville7989

    Joined:
    Aug 2, 2010
    #1
    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.
     
  2. macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #2
    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.
     
  3. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    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
     
  4. thread starter macrumors 6502a

    Grannyville7989

    Joined:
    Aug 2, 2010
    #4
    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.
     
  5. macrumors regular

    Joined:
    Sep 16, 2011
    #5
    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:
    Code:
    [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.
     
  6. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    To Samdev: You are using Pre iOS4 Transitions, which will work fine, but I encourage to use the UIAnimation code blocks ^_-
     

Share This Page