2

What I need is when a UIImageView is dragged off of the screen it to bounce back when it gets let go. I have it working in the left and top sides this is what I am doing.

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {          

        if (!CGRectContainsPoint(self.view.frame, imageView.frame.origin)){  

            CGFloat newX = 0.0f;
            CGFloat newY = 0.0f;

            // If off screen upper and left

            if (imageView.frame.origin.x < 0.0f){
                CGFloat negX = imageView.frame.origin.x * -1;
                newX = negX;
            }else{
                newX = imageView.frame.origin.x;
            }

            if (imageView.frame.origin.y < 0.0f){
                CGFloat negY = imageView.frame.origin.y * -1;
                newY = negY;
            }else{
                newY = imageView.frame.origin.y;
            }


            CGRect newPoint = CGRectMake(newX, newY, imageView.frame.size.width, imageView.frame.size.height);

            [UIView beginAnimations:@"BounceAnimations" context:nil];
            [UIView setAnimationDuration:.5];
            [letterOutOfBounds play];
            [imageView setFrame:newPoint];

            [UIView commitAnimations];

        }
    }
}

So I would like to achieve the same thing for the right and bottom sides. But I have been stuck at this for awhile. Any Ideas?

Jonathan
  • 507
  • 5
  • 15

2 Answers2

6

How about something like the following?

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UIImageView *imageView = nil;

    BOOL moved = NO;
    CGRect newPoint = imageView.frame;

    // If off screen left

    if (newPoint.origin.x < 0.0f){
        newPoint.origin.x *= -1.0;
        moved = YES;
    }

    // if off screen up

    if (newPoint.origin.y < 0.0f){
        newPoint.origin.y *= -1.0;
        moved = YES;
    }

    // if off screen right

    CGFloat howFarOffRight = (newPoint.origin.x + newPoint.size.width) - imageView.superview.frame.size.width;
    if (howFarOffRight > 0.0)
    {
        newPoint.origin.x -= howFarOffRight * 2;
        moved = YES;
    }

    // if off screen bottom

    CGFloat howFarOffBottom = (newPoint.origin.y + newPoint.size.height) - imageView.superview.frame.size.height;
    if (howFarOffBottom > 0.0)
    {
        newPoint.origin.y -= howFarOffBottom * 2;
        moved = YES;
    }

    if (moved)
    {
        [UIView beginAnimations:@"BounceAnimations" context:nil];
        [UIView setAnimationDuration:.5];
        [letterOutOfBounds play];
        [imageView setFrame:newPoint];

        [UIView commitAnimations];
    }
}

As I read your code, the logic of "if off the left side, move it back on to the view by the same distance it was off the screen." To be honest, that doesn't quite make sense to me (why, when bouncing back, does the coordinate depend upon how far off the screen it was), but I've tried to honor that in the "off screen right" and "off screen bottom" logic. Obviously my logic is using the superview of imageView to determine the width of the containing view, but if that's not appropriate, replace it with whatever is.

Edit:

I personally do this stuff with gesture recognizers, such as:

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[self.imageView addGestureRecognizer:pan];
self.imageView.userInteractionEnabled = YES;

Thus, a gesture recognizer to animate moving the image back would be:

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static CGRect originalFrame; // you could make this an ivar if you want, but just for demonstration purposes

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        originalFrame = self.imageView.frame;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translate = [gesture translationInView:gesture.view];

        CGRect newFrame = originalFrame;

        newFrame.origin.x += translate.x;
        newFrame.origin.y += translate.y;

        gesture.view.frame = newFrame;
    }
    else if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled)
    {
        CGRect newFrame = gesture.view.frame;

        newFrame.origin.x = fmaxf(newFrame.origin.x, 0.0);
        newFrame.origin.x = fminf(newFrame.origin.x, gesture.view.superview.bounds.size.width - newFrame.size.width);

        newFrame.origin.y = fmaxf(newFrame.origin.y, 0.0);
        newFrame.origin.y = fminf(newFrame.origin.y, gesture.view.superview.bounds.size.height - newFrame.size.height);

        // animate how ever you want ... I generally just do animateWithDuration

        [UIView animateWithDuration:0.5 animations:^{
            gesture.view.frame = newFrame;
        }];
    }
}

Or, if you want a gesture recognizer that just prevents the dragging of the image off the screen in the first place, it would be:

- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    static CGRect originalFrame;

    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        originalFrame = self.imageView.frame;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translate = [gesture translationInView:gesture.view];

        CGRect newFrame = originalFrame;

        newFrame.origin.x += translate.x;
        newFrame.origin.x = fmaxf(newFrame.origin.x, 0.0);
        newFrame.origin.x = fminf(newFrame.origin.x, gesture.view.superview.bounds.size.width - newFrame.size.width);

        newFrame.origin.y += translate.y;
        newFrame.origin.y = fmaxf(newFrame.origin.y, 0.0);
        newFrame.origin.y = fminf(newFrame.origin.y, gesture.view.superview.bounds.size.height - newFrame.size.height);

        gesture.view.frame = newFrame;
    }
}

By the way, in iOS 7, you can give the animation of the image view back to its original location a little bounciness by using the new animationWithDuration with the usingSpringWithDampening and initialSpringVelocity parameters:

[UIView animateWithDuration:1.0
                      delay:0.0
     usingSpringWithDamping:0.3
      initialSpringVelocity:0.1
                    options:0
                 animations:^{
                     // set the new `frame` (or update the constraint constant values that
                     // will dictate the `frame` and call `layoutViewsIfNeeded`)
                 }
                 completion:nil];

Alternatively, in iOS7, you can also use UIKit Dynamics to add a UISnapBehavior:

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.animator.delegate = self;

UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.viewToAnimate snapToPoint:CGPointMake(self.viewToAnimate.center.x, self.view.frame.size.height - 50)];

// optionally, you can control how much bouncing happens when it finishes, e.g., for a lot of bouncing:
//
// snap.damping = 0.2;

// you can also slow down the snap by adding some resistance
//
// UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc] initWithItems:@[self.viewToAnimate]];
// resistance.resistance = 20.0;
// resistance.angularResistance = 200.0;
// [self.animator addBehavior:resistance];

[self.animator addBehavior:snap];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • That works great, the only problem I am having is self.view is giving portrait dimensions when its in landscape. CGRectContainsRect(self.view.frame, imageView.frame) – Jonathan Nov 17 '12 at 04:36
  • Rotation is always an annoying problem. BTW, if my answer works for you please accept it, thanks. – sunkehappy Nov 17 '12 at 04:39
  • @Jonathan My code doesn't use `self.view.frame`, so I'm not sure I'm following you. I'm using `imageView.superview.frame`, but are you saying that's returning portrait dimensions even when in landscape? – Rob Nov 17 '12 at 04:40
  • @Rob Sorry I wasn't clear but yeah it returns portrait dimensions when it's in landscape – Jonathan Nov 17 '12 at 04:47
  • @Rob This kind of seems complated, do you have any better ideas to keep the UIImageView in the view, maybe make it so they can't even drag it off? – Jonathan Nov 17 '12 at 04:54
  • @Jonathan I didn't mean to get complicated. I was just replicating the logic you already had and adding it for the extra two dimensions. But, yes, it's probably better to stop dragging in the various dimensions when you hit the end of the screen, rather than letting them drag it off the screen and bouncing it back. – Rob Nov 17 '12 at 05:00
  • @Rob Ok so thats what I'll do. What would be the best way to do that? Thanks – Jonathan Nov 17 '12 at 05:14
  • @Jonathan I've updated my answer with some examples. Admittedly, these are both with gesture recognizers (which I think are simpler than the `touches` functions, but it's a matter of personal taste), but hopefully you can see how you could adapt these for your own purposes. And in terms of constraining where the image can be placed, you could either tweak the code, or create a subview of your view controller that defines the bounds of where the image can be dragged, and then make the imageview a subview of that. – Rob Nov 17 '12 at 05:59
  • @Jonathan In terms of your question about `self.view.frame` (or `image view.superview.frame`) not reflecting the dimensions when you go into landscape, this is an oddity (bug?) in iOS. If you have it inside a navigation controller, `self.view.frame` is updated correctly, but if you don't have a navigation controller, it isn't. So, if you don't have a navigation controller, I find that `self.view.bounds` is updated correctly (and given that we're only interested in the width, anyway, using `bounds` for the parent view is fine), so you can replace the superview `frame` references with `bounds`. – Rob Nov 17 '12 at 06:14
1

I think the easiest way is to check whether your imageView has gone out of your self.view.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {  
    if (!CGRectContainsRect(self.view.frame, imageView.frame)){
        // Your animation to bounce.
    }
}
sunkehappy
  • 8,970
  • 5
  • 44
  • 65
  • I assume the question was not only how to determine if the image has moved to a point where it's off screen, but also how to determine the new location. – Rob Nov 17 '12 at 04:43