Question

I'm working on finding intersection point of two objects.I'm using basic animation code to move two Image1 one is form left --> right & Image2 form bottom --> top.this action will perform when you long press on the screen(anywhere).

Can any help me out how to find the both are intersected / not.I just want to display alert when both are intersected.

enter image description here enter image description here

here is my code ..

-(IBAction)gesture_action:(UILongPressGestureRecognizer *)gesture
{
    if(gesture.state == UIGestureRecognizerStateBegan) {
        [self first];
        [self colide:gesture];
    } else if(gesture.state == UIGestureRecognizerStateChanged) {
        NSLog(@"middle");
    } else if(gesture.state == UIGestureRecognizerStateEnded) {
        NSLog(@"end”);
    }
}

-(BOOL)dragIsOverTrash:(UILongPressGestureRecognizer *)gesture
{
    CGPoint pointInTrash =[gesture locationInView:self.image1];
    return [self.image1 pointInside:pointInTrash withEvent:nil];
}


-(void)colide:(UILongPressGestureRecognizer *)gesture
{
    if ([self dragIsOverTrash:gesture]){
        NSLog(@" hitted the cloud ");
    } else {
        NSLog(@" not hited ");
    }
}

-(void)first
{
    [UIView animateWithDuration:4
                     animations:^{
                        image2.frame=CGRectMake(106,5,108,128);
                     } 
                     completion:nil];

    [UIView animateWithDuration:5
                     animations:^{
                         image1.frame=CGRectMake(-250,25,240,128);
                     } 
                     completion:nil];

}

my Results :----

enter image description here

plz suggest if any other better ways...to solve this problem. Thanks for your time guys ,i appreciate your help. :)

No correct solution

OTHER TIPS

HI Guys After a long Work on with my buddy's help ,i Finally did it. here is the code which will solve this problem ..using timer .then you can easily find out the results.

   -(void)callthismethod

   {

    CALayer *layer = image1.layer.presentationLayer;
    CGRect NewObjectFrame =  layer.frame;
    CALayer *layer2 = image2.layer.presentationLayer;
    CGRect SprintFrame = layer2.frame;

    if (CGRectIntersectsRect(NewObjectFrame, SprintFrame)) 
    {

        NSLog(@"Colided");
    }
 }

Thank you my friend who helped me to solve this problem once again :)

After doing some change i got solution with diffrent way hope that helps you:-

.h file

#import <UIKit/UIKit.h>

@interface animationViewController : UIViewController
{
    UIImageView *square4;
    UIImageView *square3;
}

@end

.m file

- (void)viewDidLoad
{

    float frameWidth=320;
    float frameHeight=480;


    square4 = [[UIImageView alloc] initWithFrame: CGRectMake(frameWidth/10, frameHeight/1.35, frameWidth*.1, frameWidth*.1)];
    square4.image = [UIImage imageNamed: @"square.jpg"];
    [self.view addSubview: square4];
    CGPoint origin4 = square4.center;
    CGPoint target4 = CGPointMake(square4.center.x+300, square3.center.y);
    CABasicAnimation *bounce4 = [CABasicAnimation animationWithKeyPath:@"position.x"];
    bounce4.fromValue = [NSNumber numberWithInt:origin4.x];
    bounce4.toValue = [NSNumber numberWithInt:target4.x];
    bounce4.duration = 0.5;
    bounce4.repeatCount = HUGE_VALF;
    bounce4.autoreverses = YES;
    [square4.layer addAnimation:bounce4 forKey:@"position"];




    square3 = [[UIImageView alloc] initWithFrame: CGRectMake(frameWidth/10, frameHeight/1.35, frameWidth*.1, frameWidth*.1)];
    square3.image = [UIImage imageNamed: @"square.jpg"];
    [self.view addSubview: square3];
    CGPoint origin3 = square3.center;
    CGPoint target3 = CGPointMake(square3.center.x+300, square4.center.y);
    CABasicAnimation *bounce3 = [CABasicAnimation animationWithKeyPath:@"position.y"];
    bounce3.fromValue = [NSNumber numberWithInt:origin3.x];
    bounce3.toValue = [NSNumber numberWithInt:target3.x+50];
    bounce3.duration = 1.3;
    bounce3.repeatCount = HUGE_VALF;
    bounce3.autoreverses = YES;
    [square3.layer addAnimation:bounce3 forKey:@"position"];

    [super viewDidLoad];


    [NSTimer scheduledTimerWithTimeInterval: 0.5
                                     target: self
                                   selector: @selector(checkCollision:)
                                   userInfo: nil
                                    repeats: YES];


}


-(void) checkCollision: (NSTimer *) theTimer{


    if(CGRectIntersectsRect(((CALayer*)square3.layer.presentationLayer).frame,
                           ((CALayer*)square4.layer.presentationLayer).frame)) {
        //handle the collision
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"wow!"
                                                        message:@"detect Failed!"
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        NSLog(@"detect hit");

    }

}

it's OUTPUT like this:-

enter image description here

For testing i am using NStimer you can modify as par your requirement hope this helps you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top