2

두 객체의 교차점을 찾는 중입니다. 기본 애니메이션 코드를 사용하여 두 이미지 1을 왼쪽으로 이동합니다 -> 오른쪽 & 이미지 2 양식 맨 아래 -> 맨 위. 길게 누르면 화면 (어디서나)에.xcode에서 애니메이션을 사용하는 동안 교차 된/교차하지 않는 두 객체를 찾는 방법은 무엇입니까?

두 가지 방법이 교차되어 있는지 확인하는 방법을 알려줄 수 있습니까? 두 가지가 교차 할 때 경고 만 표시하고 싶습니다. ----

enter image description here

PLZ 다른 경우 제안 : 여기 enter image description here

enter image description here 내 코드 ..

-(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]; 

}

내 결과입니다 내기 이 문제를 해결하기 위해 ... 시간을내어 주셔서 감사합니다. 귀하의 도움에 감사드립니다. :)

+1

제발 당신은 실제 코드가 아닌 이미지를 추가 할 수 있습니다. – Popeye

+0

@Popeye 여기 코드 http://pastie.org/8856832의 링크입니다 .. dealy buddy.can 당신이 나를 도울 수 있도록 ... –

+0

이미지 또는 링크가 아닌 질문에 코드를 입력하십시오. . 링크가 만료 될 수 있습니다. – Popeye

답변

2

안녕 얘들 아 내 친구의 도움으로 오랜 작업 후, 마침내 해냈어. 여기에이 문제를 해결할 코드가 있습니다. 타이머를 사용하면 결과를 쉽게 찾을 수 있습니다.

-(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"); 
    } 
} 

당신에게 나를 다시 한번이 문제를 해결하기 위해 :)

0

을 당신을 도와 내가 diffrent 방법 희망 솔루션을 가지고 몇 가지 변화를 수행 한 후 도움이 내 친구 감사합니다 -

.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을 사용하고 테스트하기 위해

enter image description here

.