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:-
For testing i am using NStimer you can modify as par your requirement hope this helps you.