2014-01-10 46 views
0

我有一個自定義UIView在我的視圖控制器,作爲一個「抽屜」控制的底部。大約90個像素的視圖是可見的。當用戶點擊視圖時,它向上顯示動畫,顯示其他選項,控件等。再次點擊後,視圖會回退到其原始位置。有效地「關閉抽屜」。如何正確實現一個可拖動的「抽屜」自定義UIView

我也實現代碼,以便查看跟蹤與拖動手勢:

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

    if(self.frame.origin.y >= 80) { 
     UITouch *theTouch = [touches anyObject]; 
     CGPoint location = [theTouch locationInView:self]; 
     CGPoint previousLocation = [theTouch previousLocationInView:self]; 
     CGFloat target = location.y - previousLocation.y; 
     self.frame = CGRectOffset(self.frame, 0, target); 
    } 
} 

所有這一切都按預期工作。但是,該視圖對於快速「輕彈」手勢沒有正確響應。在touchesEnded:withEvent:方法中,我將用戶將手指擡到目的地的視圖動畫化。如果用戶拖動緩慢然後釋放,這很好。但是,如果他們快速輕彈,視圖仍然需要處理0.5秒的時間,這對眼睛來說並不順利。以下是所有自定義視圖的代碼:

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

    if(self.frame.origin.y >= 80) { 
     UITouch *theTouch = [touches anyObject]; 
     CGPoint location = [theTouch locationInView:self]; 
     CGPoint previousLocation = [theTouch previousLocationInView:self]; 
     CGFloat target = location.y - previousLocation.y; 
     self.frame = CGRectOffset(self.frame, 0, target); 
    } 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(self.frame.origin.y == kDefaultUpY) { 
     _comingFromUp = YES; 
     _comingFromDown = NO; 
    } 
    else if(self.frame.origin.y == kDefaultDownY) { 
     _comingFromUp = NO; 
     _comingFromDown = YES; 
    } 
} 

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

    //responding to tap 
    if(self.frame.origin.y == kDefaultUpY) { 
     [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{ 
      self.frame = CGRectMake(0, kDefaultDownY, self.frame.size.width, self.frame.size.height); 
     } completion:nil]; 

     return; 
    } 
    else if(self.frame.origin.y == kDefaultDownY) { 
     [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{ 
      self.frame = CGRectMake(0, kDefaultUpY, self.frame.size.width, self.frame.size.height); 
     } completion:nil]; 

     return; 
    } 

    //responding to drag 
    if(_comingFromDown) { 
     if(self.frame.origin.y < kDefaultDownY) { 
      //dragging up -- go up 
      [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{ 
       self.frame = CGRectMake(0, kDefaultUpY, self.frame.size.width, self.frame.size.height); 
      } completion:nil]; 
     } 
     else if(self.frame.origin.y > kDefaultDownY) { 
      //dragging down -- stay down 
      [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{ 
       self.frame = CGRectMake(0, kDefaultDownY, self.frame.size.width, self.frame.size.height); 
      } completion:nil]; 
     } 
    } 
    else if(_comingFromUp) { 
     if(self.frame.origin.y < kDefaultUpY) { 
      //dragging up -- stay up 
      [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{ 
       self.frame = CGRectMake(0, kDefaultUpY, self.frame.size.width, self.frame.size.height); 
      } completion:nil]; 
     } 
     else if(self.frame.origin.y > kDefaultUpY) { 
      //dragging down -- go down 
      [UIView animateWithDuration:0.5f delay:0.0f usingSpringWithDamping:0.75f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseIn animations:^{ 
       self.frame = CGRectMake(0, kDefaultDownY, self.frame.size.width, self.frame.size.height); 
      } completion:nil]; 
     } 
    } 
} 

如何正確響應與「抽屜」視圖關聯的手勢?有沒有實現這種功能的標準方式?

回答

2

使用手勢識別器而不是自己實現觸摸處理代碼。您可以附加滑動手勢識別器和平移手勢識別,並設置平移手勢,以便滑動觸發前滑動必須失敗。使滑動手勢的動畫更快。類似0.1或0.2秒的東西可能適用於滑動手勢。

需要注意的是:在iOS 7中,屏幕底部的滑動手勢會觸發系統設置抽屜從屏幕底部彈出。我們的應用程序Face Dancer有一個控制抽屜,最近我們發現在iOS 7下很難滑動我們的控制抽屜,而不觸發系統設置抽屜。我們有一個用於拖動的「拇指」區域,並且我們正在添加一個輕按手勢,可以在一個步驟中完全顯示/隱藏它。

+0

很好的答案。可能也會加點擊。 – danh

+0

是什麼導致滑動手勢失敗?我有一個向上和向下滑動手勢識別器,以及一個讓滑動手柄失敗的平移手勢識別器。但平移選擇器永遠不會被調用。 – hgwhittle

+0

如果手勢太慢或角度過大,則滑動應該失敗。我自己沒有這樣做,所以我正在做一些有教養的猜測。 –

相關問題