2011-11-10 101 views
0

目前我下面這個Link到圖像上執行單指旋轉,其工作fine.But我進行旋轉後很納悶,如何進行轉換操作,如果我想移動的單指旋轉和平移從一個屏幕的位置到其他任何位置的圖像。的iOS:圖像

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // We can look at any touch object since we know we 
    // have only 1. If there were more than 1 then 
    // touchesBegan:withEvent: would have failed the recognizer. 
    UITouch *touch = [touches anyObject]; 

    // A tap can have slight movement, but we're not interested 
    // in a tap. We want more movement. So if a tap is detected 
    // fail the recognizer. 
    if ([self state] == UIGestureRecognizerStatePossible) { 
     [self setState:UIGestureRecognizerStateBegan]; 
    } else { 
     [self setState:UIGestureRecognizerStateChanged]; 
    } 

    // To rotate with one finger, we simulate a second finger. 
    // The second figure is on the opposite side of the virtual 
    // circle that represents the rotation gesture. 

    UIView *view = [self view]; 
    CGPoint center = CGPointMake(CGRectGetMidX([view bounds]), CGRectGetMidY([view bounds])); 
    CGPoint currentTouchPoint = [touch locationInView:view]; 
    CGPoint previousTouchPoint = [touch previousLocationInView:view]; 

    // use the movement of the touch to decide 
    // how much to rotate the carousel 
    CGPoint line2Start = currentTouchPoint; 
    CGPoint line1Start = previousTouchPoint; 
    CGPoint line2End = CGPointMake(center.x + (center.x - line2Start.x), center.y + (center.y - line2Start.y)); 
    CGPoint line1End = CGPointMake(center.x + (center.x - line1Start.x), center.y + (center.y - line1Start.y)); 

    ////// 
    // Calculate the angle in radians. 
    // (From: http://iphonedevelopment.blogspot.com/2009/12/better-two-finger-rotate-gesture.html) 
    CGFloat a = line1End.x - line1Start.x; 
    CGFloat b = line1End.y - line1Start.y; 
    CGFloat c = line2End.x - line2Start.x; 
    CGFloat d = line2End.y - line2Start.y; 

    CGFloat line1Slope = (line1End.y - line1Start.y)/(line1End.x - line1Start.x); 
    CGFloat line2Slope = (line2End.y - line2Start.y)/(line2End.x - line2Start.x); 

    CGFloat degs = acosf(((a*c) + (b*d))/((sqrt(a*a + b*b)) * (sqrt(c*c + d*d)))); 

    CGFloat angleInRadians = (line2Slope > line1Slope) ? degs : -degs; 
    ////// 

    [self setRotation:angleInRadians]; 
} 

回答

1

我不知道我的回答將幫助你,但我擔心你不能做「一觸式」旋轉「一觸式」翻譯在同一時間。事實上,通過「模擬」第二根手指觸摸顯示屏,您的代碼始終將旋轉解釋爲一次觸摸。

你可以做的是努力和分化兩個單指手勢這麼一個有旋轉和其他與轉換相關聯。例如,翻譯手勢可以是「長時間觸摸」(即,您觸摸並保持觸摸一段時間,然後移動該對象)。

希望這有助於反正...

+0

但我目前使用的圖像尺寸爲30 * 30,其項目requirement.On這要是我進行雙指旋轉情況並非如此properly.So什麼,我可以做什麼,任何建議或任何指針? – raaz

+0

我可以出來的唯一的事情是什麼,我說:你按(比方說,半秒鐘),然後拖動圖像;或者,您輕掃以旋轉它。看起來相當一致,應該與一個30x30圖像工作正常... – sergio

+0

長時間按下你的意思是UILongPressGestureRecognizer或我必須在UITouch事件方法中實現它.. – raaz