2010-03-18 67 views
0

我工作的一個項目,需要創建一個類似iPhone的主屏幕畫面的畫面:創建像iPhone主屏幕滾動型帶和按鈕

  • 多頁
  • A A滾動視圖一堆圖標
  • 如果沒有在編輯模式下,通過不同的頁面刷卡(即使我開始上的圖標觸摸)
  • 如果沒有在編輯模式下,點擊圖標做一些事情
  • 在編輯模式下,拖交換圖標地方,甚至交換到不同的網頁
  • 在編輯模式下,點擊圖標刪除它

以前我從幾個論壇,我要繼承的UIScrollView,以有對UIViews觸摸輸入讀取它的頂部。所以,我是子類重寫的方法來處理觸摸:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    //If not dragging, send event to next responder 
    if (!self.dragging) 
     [self.nextResponder touchesBegan:touches withEvent:event]; 
    else 
     [super touchesBegan:touches withEvent:event]; 
} 

總的來說,我已經覆蓋的touchesBegan :, touchesMoved:和touchesEnded:方法與此類似。

然後在視圖控制器,I加入到下面的代碼:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    UIView *hitView = (UIView *)touch.view; 
    if ([hitView isKindOfClass:[UIView class]]) { 
     [hitView doSomething]; 
     NSLog(@"touchesBegan"); 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    // Some codes to move the icons 
    NSLog(@"touchesMoved"); 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"touchesEnded"); 
} 

當運行該應用程序,我有方法的touchesBegan正確檢測。但是,當我試圖拖動圖標時,圖標剛剛移動了一點點,然後頁面開始滾動。在控制檯中,它僅記錄2或3個「touchesMoved」消息。但是,我從另一個項目中瞭解到,只要我還在屏幕上拖動,它應該記錄大量的「touchesMoved」消息。

(我懷疑我有delayedContentTouches設置爲YES,所以當我試圖拖動圖標時會稍微延遲一段時間後,它會發送信號返回到滾動視圖以滾動頁面。請糾正我,如果我錯了。)

所以,如果任何幫助的代碼來執行上述任務將不勝感激。我在這個地方呆了近一個星期,沒有任何希望。非常感謝。

+6

而不是試圖推出自己的實現,有你從Three20項目看TTLauncherView:http://github.com/facebook/three20/blob/master/src/TTLauncherView.m?它可能已經這樣做了。 – 2010-03-18 12:35:03

+0

這個問題的重複:http://stackoverflow.com/questions/4969263/create-springboard-like-main-view(反之亦然)。 – livingtech 2011-08-16 21:39:28

回答

0

在這裏,我一直在研究一個應用程序,就像天氣應用程序...這是你應該做的......轉到蘋果開發者網站,並尋找一個名爲PageControl的示例應用程序。然後,一旦你得到該scrolllview與你的應用程序一起工作,你應該有一個自定義的方法來計算UIViews(即圖標)和你的[UIColor colorWithPatternImage:(UIImage *)圖像]的高度和寬度,所以你可以給它一個圖像。然後你把視圖放在一個可變數組中,並且動畫我會玩CA動畫和UIView動畫,甚至是自定義。

0

如果你的目標是iOS 3.2+,我推薦使用UIGestureRecognizer。對於您的使用案例UIPanGestureRecognizer是正確的選擇。

手勢處理代碼應該是這個樣子:

 
- (void)handlePanGesture:(UIGestureRecognizer *)gestureRecognizer; 
{ 
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 
     panOffset = [gestureRecognizer locationOfTouch:0 inView:topView]; 
    } 

    if (gestureRecognizer.state == UIGestureRecognizerStateChanged) { 
     CGPoint delta = self.scrollView.contentOffset; 
      delta += ([gestureRecognizer locationOfTouch:0 inView:scrollView].x - panOffset.x); 
     self.scrollView.contentOffset = delta; 
    } 
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
       if(self.scrollView.contentOffset >= (self.scrollView.bounds.size.width/2)) { 
        // animate to the next multiple of self.scrollView.bounds.size.width 
       } else { 
        // animate to previous multiple 
       } 

    } 
}