제 제스처 인식기에 약간의 문제가 있습니다.팬 제스처가 회전에 따라 방향을 엉망으로 만듭니다.
"Sprite"라는 클래스가 있습니다.이 클래스는 UIImageView입니다. Sprite에는 사용자가 그래픽을 이동, 회전 및 크기 조정할 수 있도록 자체 제스처 인식기 및 처리 방법이 있습니다.
-(void)setup{ //sets up the imageview...
//add the image, frame, etc.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
UIRotationGestureRecognizer *rotateGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
[self addGestureRecognizer:panGesture];
[self addGestureRecognizer:pinchGesture];
[self addGestureRecognizer:rotateGesture];
}
//handling methods
-(void)handlePinch:(UIPinchGestureRecognizer *)recognizer{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
-(void)handleRotate:(UIRotationGestureRecognizer *)recognizer{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
}
-(void)handlePan:(UIPanGestureRecognizer *)recognizer{
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self]
}
그래서 기본적으로 그들 각자가 스스로 잘 작동 :
여기 내 코드입니다. 그러나 imageView를 회전하거나 크기를 조정할 때 패닝이 문제가됩니다. 예를 들어, imageView를 거꾸로 회전하면 팬 동작에 따라 이미지가 역방향으로 이동합니다 (위쪽이 아래, 왼쪽으로 드래그하면 오른쪽으로 이동 함). 마찬가지로 크기가 조정 된 스프라이트는 이전과 같은 속도/거리에서 이동하지 않습니다.
해결 방법에 대한 의견이 있으십니까? 나는이 코드를 ViewController가 아닌 Sprite 클래스 내에 유지하는 것을 선호한다 (가능한 경우). 고맙습니다.
Txs @Jerry, 좋은 대답. 이것은 나를 완전히 미치게했다! –
@Jerry, 멋지게 작동합니다 ... –