2014-06-07 1 views
1

viewDidLoad에서 deepFryingBasket이라는 imageView를 만들고 있습니다. 손가락을 좌우로 움직일 수 있지만 위아래로 움직일 수는 없습니다. 내가 가지고있는 두 번째 이미지는 화면 아래로 떨어지는 하나의 프렌치 프라이입니다. 프라이와 바스켓이 충돌하면 프라이가 사라져 바구니에 떨어지는 것처럼 보입니다. 그래서 0.01 초마다 CollisionDetection 메서드를 실행하는 NSTimer가 있습니다.CGRectContainsPoint가 YES를 반환하지 않지만 시뮬레이터에서 점이 이미지에 있음

문제는 내가 내 바구니로 프렌치 프라이를 잡을 때 프라이가 사라지지 않는다는 것입니다. collisionDetection 메서드가 호출되었지만 컴파일러가 if 문을 사용하지 못했고 그 이유를 모르겠습니다. 어떤 도움이라도 정말 감사 할 것입니다!

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    DeepFryingBasket =[[UIImageView alloc] initWithFrame:CGRectMake(110,468,100,80)]; 
    DeepFryingBasket.image = [UIImage imageNamed:@"DeepFryingBasket"]; 
    [self.view addSubview:DeepFryingBasket]; 
    CollisionDetection = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collisionDetection) userInfo:nil repeats:YES]; 
    [CollisionDetection fire]; 
} 

-(void)collisionDetection{ 
    if (CGRectContainsPoint (FrenchFrie.frame, DeepFryingBasket.center)){ 
     [FrenchFrie removeFromSuperview]; 
     points = points + 1; 
     dropped = dropped +1; 
    } 
} 

나는 다음 코드로 바구니를 이동 :

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    touchLocation = [touch locationInView:self.view]; 
    frame = DeepFryingBasket.frame; 
    if (CGRectContainsPoint(frame, touchLocation)){ 
     DeepFryingBasket.center = CGPointMake(touchLocation.x, DeepFryingBasket.center.y); 
    } 
} 

편집을 : 나는 또한

DeepFryingBasket.userInteractionEnabled = YES; 

을 추가하는 시도하지만 크게 변하지 않았다. 나는 또한 french frie가 애니메이션과 함께 떨어지는 동안 이미지의 근원은 바뀌지 않는다는 것을 발견했다. 왜 이렇게 변하지 않습니까? 나는 바구니를 움직이면 center.x가 내 손가락과 함께 움직이는 것을 알아 챘다. 는 다음 코드와 프랑스 frie 이동 :

-(void)letFrenchFrieFall { 
    [UIView animateWithDuration:10.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ 
    FrenchFrie.frame = CGRectMake(randomX, (int)[[UIScreen mainScreen] bounds].size.height + 40, FrenchFrie.frame.size.width, FrenchFrie.frame.size.height); 
    } completion:^(BOOL finished){ 
     [FrenchFrie removeFromSuperview]; 
    }]; 
} 

답변

1

OK 그래서 난 내 자신의 질문에 대한 답변을 발견 : 이미지 뷰 인해 애니메이션, x 축과로 설정되어 Y 좌표로 이동하는 동안 애니메이션이 끝나는 장소. 이러한 이유로 당신은 표시되는 프레임을 얻기 위해

FrenchFrieFrame = [[FrenchFrie.layer presentationLayer] frame]; 

을 사용할 수 있습니다

FrenchFrie.frame 

로의 위치에 도착하는 것은 불가능합니다. Quartzcore Framework를 포함하고 가져 오는 것을 잊지 마십시오.

#import <QuartzCore/QuartzCore.h> 
관련 문제