2012-03-24 2 views
0

저는 Objective-C를 처음 접했고 상당히 간단한 퍼즐을 만들려고합니다. 저는 현재 방법을 배우는 방법으로 4 조각 퍼즐을 만드는 것을보고 있습니다."드래그 앤 드롭"게임 애니메이션

현재 화면 주위에 4 개의 조각이 놓여있어서 화면의 특정 위치로 드래그하면 해당 위치로 스냅됩니다.

제 문제는 터치를 움직이려 고합니다. 이미지가 약간 확장되지만 문법에 문제가 있습니다. 나는 애플이 제공하는 MoveMe 예제를 보았지만이 상황에서 나를 돕지는 못했다. 아래는 코드입니다. 어떤 도움을 주셔서 감사합니다. 감사.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    //location of current touch 
    CGPoint location = [touch locationInView:self.view]; 
    if ([touch view] == image1) { 
     image1.center = location; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     image1.transform = CGAffineTransformMakeScale(1.5, 1.5); 
     [UIView commitAnimations];  
    } else if ([touch view] == image2) { 
     image2.center = location; 
     animateFirstTouch:image2; 
    } else if ([touch view] == image3) { 
     image3.center = location; 
     image3.transform = CGAffineTransformMakeScale(1.5, 1.5); 
    } else if ([touch view] == image4) { 
     image4.center = location; 
     image4.transform = CGAffineTransformMakeScale(1.5, 1.5); 
    } 

} 

-(void) animateFirstTouch:(UIView *)image { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    self.view.transform = CGAffineTransformMakeScale(1.5, 1.5); 
    [UIView commitAnimations]; 
} 

image1 코드가 작동하고 이미지의 크기가 0.5 초가 넘었습니다.

image2의 코드가 오류를 발생시킵니다. animateFirstTouch : image2에 대한 표현식 결과가 사용되지 않았습니다.

image3 및 image4의 코드는 애니메이션없이 이미지의 크기를 조정합니다.

답변

1

이 링크의 Kuldeep 님의 답변을 참조하십시오. 나는 당신을 위해 일 것이라고 생각 :

iPhone/iOS: How to implement Drag and Drop for subviews of a Scrollview?

는 또한 대안으로이 링크에 아이폰의 대답을 참조하십시오

iPhone App: implementation of Drag and drop images in UIView

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    //location of current touch 
    CGPoint location = [touch locationInView:self.view]; 
    if ([touch view] == image1) { 
     [self animateFirstTouch:image1 withLocation:location]; 
    } else if ([touch view] == image2) { 
     [self animateFirstTouch:image2 withLocation:location]; 
    } else if ([touch view] == image3) { 
     [self animateFirstTouch:image3 withLocation:location]; 
    } else if ([touch view] == image4) { 
     [self animateFirstTouch:image4 withLocation:location]; 
    } 

} 

-(void) animateFirstTouch:(UIImageView *)image withLocation:(CGPoint)location { 
     image.center = location; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     image.transform = CGAffineTransformMakeScale(1.5, 1.5); 
     [UIView commitAnimations]; 
} 

희망이 당신을 도와줍니다.

자세한 도움이 필요하면 저에게 연락하십시오.

+0

안녕하세요. 고마워하지만 그건 내가 정말로 원했던 것과는 전혀 다른 접근법처럼 보인다. 나는 애니메이션과 스케일링에 대해 배우려고 노력하고있다. 내가 볼 수있는 한 애니메이션 기능을 설정했습니다. 나는 이미지를 함수에 전달하는 데 문제가 있습니다. 도움이된다면 도움이 될 것입니다. – garethdn

+0

@ garethdn : 내 대답을 편집하고 코드를 수정 한 코드를 확인하고 내 대답에 추가하십시오. 해당 코드를 사용하십시오. 그것에 대해 도움이 필요하면 저에게 연락하십시오. –

+0

그 분께 감사드립니다. 이 애니메이션을 위에 기술 한 animateFirstTouch 함수와 같은 자체 메서드로 제한 할 수있는 방법이 있습니까? 동일한 코드를 반복해서 반복하는 것은 낭비스러운 것처럼 보입니다. 또는 더 나은 방법은 말할 수 있습니다 : 터치되는 객체가 이미지 인 경우 애니메이션을 0.5 초 이상으로 확대 할 수 있습니까? – garethdn