2011-12-31 63 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

相关问题