2012-05-28 32 views
6

Mã sau đây chính xác pinch/thu phóng chế độ xem vùng chứa, nhưng chỉ sau khi nó nhảy đến tỷ lệ 1.0. Làm cách nào tôi có thể sửa đổi nó để chế độ xem vùng chứa có quy mô từ tỷ lệ hiện tại của nó?ios - Chụm/thu phóng từ tỷ lệ hiện tại

UIPinchGestureRecognizer *twoFingerPinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)]; 
[self.container addGestureRecognizer:twoFingerPinch]; 

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{ 
    _scale = recognizer.scale; 
    CGAffineTransform tr = CGAffineTransformScale(self.view.transform, _scale, _scale); 
    self.container.transform = tr; 
} 
+0

bạn đã giải quyết vấn đề của bạn ? Nếu có thì vui lòng chia sẻ mã. – Manthan

Trả lời

9

Trong file .h, thêm:

CGFloat _lastScale; 

Trong file .m,

- (id)init { 
    ... 
    _lastScale = 1.0f; 
    ... 
} 

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 
{ 
    if (recognizer.state == UIGestureRecognizerStateEnded) { 
     _lastScale = 1.0f; 
     return; 
    } 

    CGFloat scale = 1.0f - (_lastScale - recognizer.scale); 
    CGAffineTransform tr = CGAffineTransformScale(self.view.transform, scale, scale); 
    self.container.transform = tr; 

    _lastScale = recognizer.scale; 
} 
+0

Điều này không hoạt động. Nó không bao giờ có tỷ lệ nhỏ hơn 1.0 – soleil

+2

_lastScale và recognizer.scale luôn giống nhau, vì vậy tỷ lệ luôn luôn rất gần 1.0, vì vậy chế độ xem chỉ vừa zoom vào hoặc ra. – soleil

2

Đây là cách tôi làm điều đó:

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer { 

    static float initialDifference = 0.0; 
    static float oldScale = 1.0; 

    if (recognizer.state == UIGestureRecognizerStateBegan){ 
     initialDifference = oldScale - recognizer.scale; 
    } 

    CGFloat scale = oldScale - (oldScale - recognizer.scale) + initialDifference; 

    myView.transform = CGAffineTransformScale(self.view.transform, scale, scale); 

    oldScale = scale;  

} 
+0

Điều này yêu cầu mã hóa nhiều hơn để thu nhỏ. –