触摸并下拉视图

我在界面顶部(状态栏下方)有一个uiview,只显示它的底部。

实际上,我想让红色uiview向下滑动以完全显示拖动,例如本机iOS中的notificationcenter,而不仅仅是通过点击按钮。

我应该用什么来“触摸和拉下”uiview,以便完全显示?

例

创建UIView的子类。

覆盖touchesBegan:withEventtouchesMoved:withEvent

touchesBegan可能进行视觉上的改变,以便用户知道他们正在触摸视图。 在touchesMoved中使用[[touches anyObject] locationInView:self][[touches anyObject] previousLocationInView:self]

计算当前触摸位置和最后触摸位置之间的差异(检测向下拖动或向后拖动)。

然后,如果你是自定义绘图,请调用[self setNeedsDisplay]告诉你的视图重绘它的drawRect:(CGRect)rect方法。

注意:这假定此视图不使用多次触摸。

无需找到拖放式解决方法。 UIScrollView可以在没有任何性能损失的情况下进行操作。

拉下

 @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)]; 

这是一个简单的例子。 您可以添加任何function,例如在可拖动区域的底部添加标题按钮栏以实现单击删除,或者向界面添加一些方法以由调用者重新定位它。

请参阅我在iPhone App中的答案:在UIView中拖放图像的实现

您只需要使用TouchesBeginTouchesEnded方法。 在该示例中,我已经展示了如何使用CGPoint ,而不是必须尝试使用setFramedrawRect作为视图。

一旦调用了TouchesMoved方法,你必须使用setFramedrawRect (不确定但是哪个有效,主要是setFrame)也从CGPoint获取高度。