2011-11-19 73 views
2

我在使用CGAffineTransformMakeRotation時發生跳躍運動的問題。它似乎工作正常,直到我到達象限1和2的頂部。下面是我使用的代碼和youtube鏈接,顯示發生了什麼。任何洞察力將非常感激。使用CGAffineTransformMakeRotation時的抖動運動

我的目標是旋轉一個UIWebView裏面的svg。由於我無法輕易地在UIWebView上單獨檢測觸摸,所以我在其上放置了一個空白的UIImageView。這使我可以檢測到觸摸並防止彈出複製對話框。

http://www.youtube.com/watch?v=x_OmS0MPdEE&feature=youtu.be

- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event { 
[super touchesBegan:touches withEvent:event]; 
NSArray *allTouches = [touches allObjects]; 
CGPoint location = [[allTouches objectAtIndex:0] locationInView:self.view]; 
if(selected == 1) { 
    CGFloat rads = atan2f(location.y - (grid1.frame.size.height/2),location.x - (grid1.frame.size.width/2)); 
    grid1.transform = grid1Btn.transform = CGAffineTransformMakeRotation(rads); 
} 

    } 

    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
[super touchesBegan:touches withEvent:event]; 
NSArray *allTouches = [touches allObjects]; 
CGPoint location = [[allTouches objectAtIndex:0] locationInView:self.view]; 

if(CGRectContainsPoint(grid1Btn.frame, location)) 
{ 
    selected = 1; 
} 
    } 

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     selected = -1; 
    } 

回答

0

在計算中使用了一些frame.size.*。請注意,這些值(與bounds.size.*不同)受transform的影響。改爲使用grid1.center。這也更合乎邏輯(你圍繞中心旋轉)。

你需要修復的下一件事是,它不應該跳轉到新的位置時,觸摸啓動:)

+0

知府,這樣一個簡單的解決方案。非常感謝! 我通過刪除touchesBegan中的變換來阻止它在開始時跳躍。 –

0

急動是由觸摸事件位置的誤差的固有引起的。 位於框架中心附近的觸摸事件很可能會偏離框架的對角相對象,而圍繞框架中心繪製的是圓。 這會導致視頻中觀察到的90度突然跳躍。 避免此問題的一種方法是在框架中心引入一個死區。位於框架中心給定半徑內的任何觸摸都不會觸發重新計算的旋轉。

希望這會有所幫助。