2009-06-11 24 views
16

試圖獲得一些iPhone應用程序的基本拖放功能。iPhone拖放

我對試圖做到這一點是如下當前代碼:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    UITouch *touch = [touches anyObject]; 

    CGPoint location = [touch locationInView:self]; 
    self.center = location; 
} 

這段代碼有一個觸摸的UIView閃爍,同時它遵循以觸摸的結果。在玩了一段時間之後,我還注意到UIView似乎從屏幕上的0,0位置閃爍到當前觸摸的位置。

任何想法我做錯了什麼?

+0

哪個視圖在這?你的應用有哪些視圖? – 2009-06-11 06:29:49

+0

應用程序使用單個視圖進行渲染,並使用交互式組件的子視圖。此代碼位於從UIImageView繼承的類實現中。 – Simon 2009-06-11 06:46:27

回答

21

中心屬性需要在超視圖的座標系中指定,但是您已根據子視圖的位置詢問觸摸事件。相反,要求他們根據超級觀點的座標,如下所示:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:self.superview]; // <--- note self.superview 

    self.center = location; 
}