2011-07-08 28 views
0

我有一個觀點的控制和內部應用程序我打算把如按鈕文本框等一些控件...我可以沿x軸拖我的看法一樣:拖動的UIView喜歡在主屏幕iPhone

1)

enter image description here

2)

enter image description here

用下面的代碼:

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

    if([touch view] == ViewMain) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     displaceX = location.x - ViewMain.center.x;   
     displaceY = ViewMain.center.y;   
     startPosX = location.x - displaceX; 
    } 

    CurrentTime = [[NSDate date] timeIntervalSince1970]; 

} 


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 



    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch view] == ViewMain) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     location.x =location.x - displaceX; 
     location.y = displaceY; 
     ViewMain.center = location; 
    } 


} 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

    double time = [[NSDate date] timeIntervalSince1970]-CurrentTime; 


    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch view] == ViewMain) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     location.x =location.x - displaceX; 
     location.y = displaceY; 
     ViewMain.center = location; 
     double speed = (ViewMain.center.x-startPosX)/(time*2); 
     NSLog(@"speed: %f", speed); 

    } 
} 

不是我要補充的全局變量:

float displaceX = 0; 
float displaceY = 0; 
float startPosX = 0; 
float startPosY = 0; 
double CurrentTime; 

爲什麼我創建的那些變量的原因是,當我開始拖動的觀點認爲從點上移動時,我觸摸它,而不是從中間。

無論如何,如果我觸摸按鈕或圖像,即使圖像在背景上具有透明度,視圖也不會拖動。我希望能夠繼續拖動視圖,而不管視圖頂部是否有圖像。我在那裏想,也許我需要把一切之上的大型透明的看法,但我需要有按鈕,圖片等等。我希望能夠拖動一個視圖只是像您可以:

enter image description here

請注意,無論我第一次觸摸應用/圖片還是文本,我都能夠拖動該視圖。我怎麼能這樣做?

回答

1

我認爲你的問題是,如果你觸摸UIButton或UIImageView與交互功能啓用,它不會傳遞觸摸。 對於圖像,取消選中IB中的User Interaction Enabled屬性。 對於導致touchesBegan:withEvent:等不被調用的按鈕,請查看以下鏈接:Is there a way to pass touches through on the iPhone?

1

您可能想要考慮針對此問題的不同方法。而不是嘗試手動管理自己滾動的內容,您可能會更好使用UIScrollView並將pagingEnabled屬性設置爲YES。這是蘋果推薦的方法(也可能是上一次截圖中Springboard.app使用的方法)。如果您是iOS開發人員計劃的成員,請查看UIScrollView上的WWDC 2010會話以獲取相關示例。我認爲他們也可能在developer.apple.com上發佈了示例代碼。