2011-12-31 64 views
5

我在界面頂部(狀態欄下方)只有底部顯示了一個uview。觸摸並拉下一個視圖

實際上,我想讓紅色uiview向下滑動,完全通過在本機iOS中拖動(如通知中心)來顯示,而不僅僅是通過點擊按鈕。

我應該如何「觸摸並拉下」uiview,才能完全顯示它?

Example

回答

3

使UIView一個子類。

替代touchesBegan:withEventtouchesMoved:withEvent

touchesBegan也許做了一個視覺變化,所以用戶知道他們正在觸摸視圖。 在touchesMoved使用 [[touches anyObject] locationInView:self][[touches anyObject] previousLocationInView:self]

計算當前觸摸位置和最後的觸摸位置(檢測向下拖動或拖動備份)之間的差。

然後,如果您是自定義繪圖,請致電[self setNeedsDisplay]告訴您的視圖重新繪製它的drawRect:(CGRect)rect方法。

注意:這裏假定多視圖不被這個視圖使用。

1

請參閱我的答案在iPhone App: implementation of Drag and drop images in UIView

你只需要使用TouchesBeginTouchesEnded方法。在這個例子中,我已經展示瞭如何使用CGPoint,而不是你必須嘗試使用​​setFramedrawRect爲你的看法。

只要TouchesMoved方法被調用,你必須使用setFramedrawRect(不知道但以往的作品,大多是SETFRAME)也採取了高度的CGPoint

+0

請問 - (void)pan:(UIPanGestureRecognizer *)手勢是否可以勝任? – Alby 2012-01-02 10:31:46

+1

是的,但在使用它時必須更加小心......因爲你需要定義適當的控件和其他東西......這也很好......但是如果你使用Touchesbegin和TouchesEnded ...那麼你可以簡單地在Interface Builder中給IBOutlets ...... – DShah 2012-01-02 12:20:02

9

無需找到拖放的解決方法。一個UIScrollView可以做到這一點,而不會因聽音樂而造成任何性能損失。

pulldown

@interface PulldownView : UIScrollView 

@end 

@implementation PulldownView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (!self) { 
     return self; 
    } 
    self.pagingEnabled = YES; 
    self.bounces = NO; 
    self.showsVerticalScrollIndicator = NO; 
    [self setBackgroundColor:[UIColor clearColor]]; 
    double pixelsOutside = 20;// How many pixels left outside. 
    self.contentSize = CGSizeMake(320, frame.size.height * 2 - pixelsOutside); 
    // redArea is the draggable area in red. 
    UIView *redArea = [[UIView alloc] initWithFrame:frame]; 
    redArea.backgroundColor = [UIColor redColor]; 
    [self addSubview:redArea]; 
    return self; 
} 

// What this method does is to make sure that the user can only drag the view from inside the area in red. 
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    if (point.y > height) 
    { 
     // Leaving useless touches to the views under it. 
     return nil; 
    } 
    return [super hitTest:point withEvent:event]; 
} 

@end 

使用方法:
1.初始化PulldownView的一個實例。
2.使用[addSubview:]將想要顯示的任何內容添加到實例。
3.以紅色隱藏該區域。

[pulldownView setContentOffset:CGPointMake(0, heightOfTheView - pixelsOutside)]; 

這是一個簡單的例子。您可以爲其添加任何功能,例如在可拖動區域的底部添加標題按鈕欄以實現點擊滴下,或向界面添加一些方法以便由調用者重新定位。

+0

這真是太棒了,我需要。謝謝! – bcattle 2013-10-04 03:57:34

相關問題