我正在寻找两个物体的交点。我正在使用基本动画代码移动两个Image1,一个是左边的形式 - >对& Image2表单底部 - > top.t这个动作将在你长按屏幕(任何地方)时执行。
可以帮助我找出如何找到两者相交/不相交。我只想在两者相交时显示警示。
这是我的代码..
-(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];
}
我的结果:----
PLZ建议是否还有其他更好的方法......来解决这个问题。 谢谢你的时间,我感谢你的帮助。 :)
答案 0 :(得分:2)
HI伙计经过长时间的工作,我的伙伴的帮助,我终于做到了。这里是解决这个问题的代码..使用计时器。然后你可以很容易地找到结果。
-(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");
}
}
谢谢我的朋友帮助我再次解决这个问题:)
答案 1 :(得分:0)
在做了一些改变之后,我得到了不同的解决方案希望可以帮助你: -
.h文件
#import <UIKit/UIKit.h>
@interface animationViewController : UIViewController
{
UIImageView *square4;
UIImageView *square3;
}
@end
.m文件
- (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");
}
}
它的输出如下: -
对于我正在使用NStimer的测试,您可以修改为您的要求希望这可以帮助您。