2011-10-03 3 views
0

내가 다음 코드 작성 :UIImageView 위치 확인 문제가 있습니까?

-(void)viewDidLoad{ 
JumpVelocity = 10; 

aStandRightArray = [[NSArray alloc] initWithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aStandR1" ofType:@"png"]], 
                [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aStandR2" ofType:@"png"]], 
                [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aStandR3" ofType:@"png"]], 
                [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aStandR2" ofType:@"png"]], 
                nil]; 

aJumpRightArray = [[NSArray alloc] initWithObjects:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aJumpR" ofType:@"png"]], nil]; 

aStandRight = [[UIImageView alloc] initWithFrame:CGRectMake(0, 242, 55, 65)]; 
aStandRight.animationImages = aStandRightArray; 
aStandRight.animationDuration = 0.5; 
[self.view addSubview:aStandRight]; 

aJumpRight = [[UIImageView alloc] initWithFrame:CGRectMake(0, 234, 69, 65)]; 
aJumpRight.animationImages = aJumpRightArray; 
[self.view addSubview:aJumpRight];} 

-(IBAction)buttonJumpRight:(id)sender{ 
[aStandRight stopAnimating]; 
[aStandRight removeFromSuperview]; 
[aJumpRight startAnimating]; 

jumpTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(playerJumpRight) userInfo:nil repeats:YES];} 

-(void)playerJumpRight{ 
[aJumpRight removeFromSuperview]; 
aJumpRight.center = CGPointMake(aJumpRight.center.x + JumpVelocity, 234); 
[self.view addSubview:aJumpRight]; 

if(aJumpRight.center.x >= 84.0) 
{ 
    [jumpTimer invalidate]; 
    jumpTimer = nil; 
    [aJumpRight stopAnimating]; 
    aStandRight.center = CGPointMake(84, 242); 
    [aStandRight startAnimating]; 
    [self.view addSubview:aStandRight]; 
} 

} 기본적으로

내가 여기서 뭘하려고하는 것은 서 애니메이션을로드입니다, 다음 사용자는 내가 서 애니메이션을 언로드 한 후 중지 buttonJumpRight 버튼을 누를 때 . 그 후 점프 애니메이션을로드하고 playerJumpRight 함수를 사용하여 화면을 가로 질러 움직이기 시작합니다.

모든이 개를 제외하고 잘 작동하는 것 같다 :

  1. 점프 애니메이션 이동은 x 축을 따라하지만 코드에서 위의 "234입니다 원래의 y 위치를 유지하지 않습니다 어떤 이유로 예상처럼 ".

  2. 점프 애니메이션 x 위치는 (84, 242)의 욕망 위치에서 다른 곳으로 문 모든 것이 서 애니메이션 에 대한 기대 새로 만든 위치처럼 작동하는 경우입니다 방법에 대한 요구 사항을 충족합니다.

은 내가 여러 가지 가능한 해결 방법을 시도하고, 꽤 많은 시간을 검색하지만 모든 시도에서 실패하고. 제 ios/objective c를 코딩하기 시작하면서 신비를 용서하십시오.

당신이 제공 할 수있는 도움을 크게 주시면 감사하겠습니다.

답변

1

수퍼 뷰에서 하위 뷰를 제거하면 center 속성은 수퍼 뷰의 좌표계를 참조하므로 의미가 없습니다. 제거하고 다시 superview에서 aJumpRight을 추가하지 마십시오, 단지 그것의 center 속성을 수정하면, 이것은 내가 생각하는대로 당신이 무엇인지 따라 이동합니다.

블록 기반 애니메이션을 사용하여 변경 사항을 center에 적용 할 수 있습니다. 자세한 내용은 UIView 클래스 참조 here을 참조하십시오.

또한보기의 centerframe.origin과 혼동시킬 수 있습니다. 후자는보기의 왼쪽 위에 있습니다. 나는 superview없이보기를 위해 center을 설정하는 것이 어떤 효과가 있다고 생각하지 않지만, 나는 그것에 대해 확신하지 못한다. 이 상황에서 프레임 원점을 확실히 설정할 수 있습니다.

+0

정확하게 맞습니다. 중심을 frame.origin과 혼동 시켰습니다. 지난 2 일 동안이 문제로 고생 해 줘서 고마워. 또한 superview에서 제거하지에 대한 팁 주셔서 감사합니다. 나는 내가 superview에서 그것을 제거하고 내가 그것을 가지고 있었던 위치 확인 문제를 고쳐 줄 것이라는 점을 볼 때 그것을 추가 할 때 중심으로 바꿨다 고 생각했다. 그러나 실제로 프레임 원점과 함께 중심의 혼란이었다. 다시 한번 감사드립니다. – RickyTheCoder

관련 문제