2012-01-08 34 views
8

Tôi gặp sự cố nhỏ với trình nhận dạng cử chỉ của mình.Cử chỉ di chuyển lộn xộn theo hướng xoay vòng

Tôi có một lớp được gọi là "Sprite" chỉ là một UIImageView. Sprite có công cụ nhận dạng cử chỉ và phương thức xử lý riêng để người dùng có thể xoay, xoay và thay đổi kích thước đồ họa.

Đây là mã của tôi:

-(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] 
} 

Vì vậy, về cơ bản mỗi người trong số họ hoạt động tốt trên riêng của họ. Tuy nhiên, khi tôi xoay hoặc thay đổi kích thước của imageView, việc panning trở nên có vấn đề. Ví dụ: nếu bạn xoay hình ảnh ngược xuống, thì cử chỉ di chuyển sẽ di chuyển hình ảnh theo hướng ngược lại (lên xuống, kéo trái di chuyển hình ảnh sang phải, v.v.). Tương tự như vậy, một sprite đã thay đổi kích thước sẽ không xoay ở cùng tốc độ/khoảng cách như trước.

Bất kỳ ý tưởng nào về cách khắc phục sự cố này? Tôi muốn giữ mã này trong lớp Sprite hơn là ViewController (nếu có thể). Cảm ơn bạn.

Trả lời

12

Thay vì dịchInView: self, try translationInView: self.superview.

+0

Txs @Jerry, câu trả lời tuyệt vời. Điều này khiến tôi phát điên! –

+0

@ Jerry, Nó hoạt động rất tốt ... –

Các vấn đề liên quan