2013-07-03 68 views
2

我有一個UIViewController被調用UIView類,和我使用CGAffine變換使用下面的代碼的iOS - 捏縮放 - 利用CGAffineTransform - 一個點

CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale); 
NSLog(@"Pinch scale: %f", recognizer.scale); 
float scale = recognizer.scale; 
float SCALE_MIN = 1.0f; 
float SCALE_MAX = 3.0f; 
. . . 
放大到我UIView類的實例

這個基本的代碼,工作! (一些代碼已被省略)

現在的問題是無論屏幕上的哪個位置,我「捏」文件總是從文件中間放大,而不是在我捏的位置。 。

任何關於如何讓它放大到精確位置捏的建議?

+0

對於那些有興趣誰發現了一個很好的解決方案,以平滑縮放:@保羅Solts答案 - HTTP:/ /stackoverflow.com/questions/5150642/max-min-scale-of-pinch-zoom-in-uipinchgesturerecognizer-iphone-ios 也有類似的討論線程:http://stackoverflow.com/questions/4947148/uipinchgesturerecognizer-兩手指之間的夾捏視圖 –

回答

1

當在UIPinchGestureRecognizer的實例上調用時,所有UIGestureRecognizers上的locationInView:方法將爲您提供組成掐的兩個觸摸之間的中點。這是我認爲你會得到的最接近的東西,「確切的位置捏。」

當手勢第一次被識別時,保存locationInView以獲取要放大的點。我不會嘗試在捏合過程中更新縮放中心,因爲我認爲你最終會遇到一個複雜的實現,這種實現不直觀。您需要使用縮放功能來保持該中心點不變,並且您應該可以通過設置您正在轉換的視圖的的anchorPoint來實現。請注意,anchorPoint座標是標準化的 - 默認值爲(0.5, 0.5),視圖的中心。你

+0

感謝您的詳細解答,您可以舉一個例子說明如何o改變定位點? –

-1

希望這將有所幫助這對我的工作就像一個魅力...

- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer 
{ 
    CGAffineTransform transform = CGAffineTransformScale(pinchRecognizer.view.transform, pinchRecognizer.scale,pinchRecognizer.scale); 
    _designImage.transform = transform; 
    _Zoomscale = pinchRecognizer.scale; 

    NSLog(@"image Pinch scale: %2ff", _Zoomscale); 
} 
0
- (void)pinchDetected:(UIPinchGestureRecognizer *)gestureRecognizer { 

if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
    // Reset the last scale, necessary if there are multiple objects with different scales 
    lastScale = [gestureRecognizer scale]; 
} 

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || 
    [gestureRecognizer state] == UIGestureRecognizerStateChanged) { 

    CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue]; 

    // Constants to adjust the max/min values of zoom 
    const CGFloat kMaxScale = 2.2; 
    const CGFloat kMinScale = 0.64; 

    CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]); 
    newScale = MIN(newScale, kMaxScale/currentScale); 
    newScale = MAX(newScale, kMinScale/currentScale); 
    CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale); 
    [gestureRecognizer view].transform = transform; 

    [gestureRecognizer setScale:1.0]; 

    lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call 
} 
}