Pan Gesture Recognizer Collision

Discussion in 'iOS Programming' started by ahan.tm, Dec 4, 2011.

  1. ahan.tm, Dec 4, 2011
    Last edited: Dec 4, 2011

    ahan.tm macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #1
    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
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    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.
     
  3. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #3
    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;
        }
     
  4. ArtOfWarfare, Dec 5, 2011
    Last edited: Dec 9, 2011

    ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    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)
     
  5. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #5
    What Do You Mean?
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    I mean if you want it to go back to its starting position, you need to enter its starting position, not (38,42)
     
  7. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #7
    (38,42) is the starting position. I have checked that.
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    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>);
     

    Attached Files:

  9. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #9
    (38,42 is correct). This used to work fine when I used touchesEnded.
     

Share This Page