Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hi,

I use a UIPanGestureRecognizer to move 6 image(UIImageViews) on the screen, I was wondering how to detect a collision with another image(non-moving/fixed) on the screen so that code is run. The code is different for each of the 6 images. I also am wondering how to move the object smoothly using animations back to the starting position.

Thanks,
ahan.tm
 
Last edited:
you're going to want to look into CGGeometry kinds of things for collision detection (I think... that's how I handle it, anyways.)

Something like
Code:
if (CGRectIntersectsRect(imageView1.frame, imageView2.frame))
Should be helpful for collision detection.

As far as smooth animations, you'll want something like:
Code:
[UIView animateWithDuration:0.5 animations:^(void)
{
// Code that describes how everything should look when the animation is finished.
}]

Stanford's iOS programming lessons, available for free via iTunes U, have a pretty good one on animations, I suggest looking into that.
 
you're going to want to look into CGGeometry kinds of things for collision detection (I think... that's how I handle it, anyways.)

Something like
Code:
if (CGRectIntersectsRect(imageView1.frame, imageView2.frame))
Should be helpful for collision detection.

As far as smooth animations, you'll want something like:
Code:
[UIView animateWithDuration:0.5 animations:^(void)
{
// Code that describes how everything should look when the animation is finished.
}]

Stanford's iOS programming lessons, available for free via iTunes U, have a pretty good one on animations, I suggest looking into that.

Okay,
I tried your code but the image does not move back to the original start position. It moves to random places on the screen. I am using
Code:
image.center = CGPointMake(38,42);

I got that code from interface builder.

Here is the full code:

Code:
if (CGRectIntersectsRect(striker.frame, plusone.frame)) { //Plus One Collision
        
        
        [UIView animateWithDuration:0.5 animations:^(void)
        {
            plusone.center = CGPointMake(38, 42);
        }];
        
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //iPhone Vibrate
        NSNumber *marbleNumber = [NSNumber numberWithInt:marbles];
        
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
                                                 (CFStringRef) @"1 Marble", CFSTR ("aif"), NULL); //Sound Stuff
        
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);

        
        marbles = 1+marbles; // Add 1 To Marbles
        marblesleft = marblesneeded - marbles;
        marblesNeeded.text = [NSString stringWithFormat:@"%i", marblesleft];
               
        [[records objectAtIndex:pageControl.currentPage] setValue:marbleNumber forKey:@"marbles"]; //Set Marble Value
        [self checkPrizeReached];
        NSError *error;
        [_managedObjectContext save:&error]; //Save 
        
                marblebeingdragged = NO;
    }
 
Reading your code, it looks to me like plus one should be moving so that its center is at (38,42), not a random point, each time it collides with striker. So... Change (38,42) to whatever is proper...

Keep in mind that the center is not a corner or the origin of the image, which might be what you meant to set to (38, 42)
 
Last edited:
Reading your code, it looks to me like pluson should be moving so that its center is at (38,42), not a random point, each time it collides with striker. So... Change (38,42) to whatever is proper...

Keep in mind that the center is not a corner or the origin of the image, which might be what you meant to set to (38, 42)

What Do You Mean?
 
(38,42) is the starting position. I have checked that.

If you've designed your interface in a .xib file, open that up, click on the button named plus one, and look to the right half of the screen. You should see something like the image I've attached to this post. Click on the center dot of the white box labeled "Origin". At this point, the starting center X and Y coordinates of plus one will be displayed. To reset your view, you'll want

plusone.center = CGPointMake(<whatever the initial center x coordinate was>, <whatever the initial center y coordinate was>);
 

Attachments

  • origin.png
    origin.png
    26.5 KB · Views: 629
If you've designed your interface in a .xib file, open that up, click on the button named plus one, and look to the right half of the screen. You should see something like the image I've attached to this post. Click on the center dot of the white box labeled "Origin". At this point, the starting center X and Y coordinates of plus one will be displayed. To reset your view, you'll want

plusone.center = CGPointMake(<whatever the initial center x coordinate was>, <whatever the initial center y coordinate was>);

(38,42 is correct). This used to work fine when I used touchesEnded.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.