Iphone 触摸并下拉视图
我在界面顶部(状态栏下方)有一个uiview,它只显示界面的底部 实际上,我想让红色的uiview向下滑动,完全通过拖动来显示,比如本机iOS中的notificationcenter,而不仅仅是点击按钮 我应该使用什么来“触摸并拉下”uiview,以便它可以完全显示Iphone 触摸并下拉视图,iphone,ios,ipad,uinavigationcontroller,uitabbarcontroller,Iphone,Ios,Ipad,Uinavigationcontroller,Uitabbarcontroller,我在界面顶部(状态栏下方)有一个uiview,它只显示界面的底部 实际上,我想让红色的uiview向下滑动,完全通过拖动来显示,比如本机iOS中的notificationcenter,而不仅仅是点击按钮 我应该使用什么来“触摸并拉下”uiview,以便它可以完全显示 创建UIView的子类 覆盖触摸开始:withEvent和触摸移动:withEvent 在触摸开始中,可能会进行视觉更改,以便用户知道他们正在触摸视图。 在触摸屏上移动使用 [[触摸任何对象]位置查看:self] 及 [[touch
创建
UIView
的子类
覆盖触摸开始:withEvent
和触摸移动:withEvent
在触摸开始
中,可能会进行视觉更改,以便用户知道他们正在触摸视图。
在触摸屏上移动使用
[[触摸任何对象]位置查看:self]
及
[[toucheanyobject]previousLocationInView:self]
计算当前触摸位置和上次触摸位置之间的差值(检测向下拖动或向上拖动)
然后,如果您是自定义绘图,请调用[self-setNeedsDisplay]
告诉您的视图在其drawRect:(CGRect)rect
方法中重新绘制
注意:这假设此视图不使用多点触摸。请参阅中的我的答案
您只需要使用touchesbeagin
和touchesend
方法。在该示例中,我演示了如何使用CGPoint
,而不是尝试使用setFrame
或drawRect
进行查看
一旦调用了
TouchesMoved
方法,您就必须使用setFrame
或drawRect
(不确定但哪种方法有效,主要是setFrame)也从CGPoint
获取高度,无需找到拖放的解决方法。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)];
这是一个简单的例子。您可以向其添加任何功能,例如在可拖动区域的底部添加一个标题按钮栏以实现单击n-drop,或者向界面添加一些方法以由调用者重新定位它。是否-(void)pan:(UIPanGestureRecognizer*)手势可以完成任务?是的,但是在使用它时必须更加小心。。。因为你需要定义适当的控制和其他东西。。。它也很好。。。但是如果您使用的是Touchesbegin和TouchesEnded。。。然后你可以简单地在Interface Builder中提供IBOutlets…这太棒了,正是我所需要的。谢谢