Alb*_*lby 5 iphone uitabbarcontroller uinavigationcontroller ipad ios
我在界面顶部(状态栏下方)有一个uiview,只显示它的底部.
实际上,我想让红色uiview向下滑动以完全显示拖动,例如本机iOS中的notificationcenter,而不仅仅是通过点击按钮.
我应该用什么来"触摸和拉下"uiview,以便完全显示?
无需找到拖放式解决方法.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
Run Code Online (Sandbox Code Playgroud)
使用方法:
1.初始化PulldownView实例.
2.使用[addSubview:]将要显示的任何内容添加到实例.
3.将区域隐藏为红色.
[pulldownView setContentOffset:CGPointMake(0, heightOfTheView - pixelsOutside)];
Run Code Online (Sandbox Code Playgroud)
这是一个简单的例子.您可以添加任何功能,例如在可拖动区域的底部添加标题按钮栏以实现单击删除,或者向界面添加一些方法以由调用者重新定位它.
创建 的子类UIView
。
覆盖touchesBegan:withEvent
和touchesMoved:withEvent
。
也许touchesBegan
可以进行视觉更改,以便用户知道他们正在触摸视图。在touchesMoved中使用
[[touches anyObject] locationInView:self]
和
[[touches anyObject] previousLocationInView:self]
计算当前触摸位置与上次触摸位置之间的差异(检测向下拖动或向上拖动)。
然后,如果您是自定义绘图,请调用[self setNeedsDisplay]
以告诉您的视图在其方法中重绘drawRect:(CGRect)rect
。
注意:这假设该视图不使用多点触摸。
归档时间: |
|
查看次数: |
4486 次 |
最近记录: |