2011-10-25 6 views
1

"고정 된"이미지가 손가락으로 움직여서 원래 위치로 되돌아가는 간단한 앱을 만들려고합니다. 이것은 아마도 코드로 더 잘 설명 될 것입니다 :CGAffineTransform 상호 작용 이해하기

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    image.transform = CGAffineTransformIdentity; 
} 


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 
    if (CGRectContainsPoint([image frame], [touch locationInView:nil])) 
    { 
     image.center = [touch locationInView:nil]; 
    } 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (pin) { 
     CGPoint point = image.center; 
     CGPoint center = self.view.center; 
     //CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 0); 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.5]; 
     image.transform = CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y); 
     //image.transform = CGAffineTransformConcat(image.transform, CGAffineTransformMakeTranslation(center.x - point.x, center.y - point.y)); 
    [UIView commitAnimations]; 

    } 
} 

이미지를 누를 때마다 손가락 아래에서 밖으로 움직 이도록 이미지가 누를 때마다 움직입니다. 나는 그것이 변형과 관련이 있다고 생각합니다. 누구든지 올바른 방향으로 나를 가리킬 수 있었습니까?

답변

1

실제로 이런 식으로 할 거라고 편집 :

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
} 


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint location = [[touches anyObject] locationInView:[touch view]]; 
    CGPoint difference = CGPointMake(location.x - image.center.x, location.y - image.center.y); 

    image.transform = CGAffineTransformTranslate(image.transform, difference.x, difference.y); 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (pin) { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.5]; 
     image.transform = CGAffineTransformIdentity; 
     [UIView commitAnimations]; 
    } 
} 
+0

잘 했어! UITouch * touch = [[allTouches] anyObject]]를 추가해야했습니다. 코드가 작동하려면 (또한 경계를 확인해야합니다). 고맙습니다! – Eric

3

난 그냥

image.transform = CGAffineTransformMakeTranslation(difference.x, difference.y); 

방법 당신은 당신이 더 축적하고 그것을하고있어 사용한다고 생각합니다 touchesMoved의 모든 반복 반복. center 속성은 내가 생각하는 변환에 의존하지 않습니다.

+0

두 가지 다른 출처 : P. 고맙습니다! – Eric