我在我的iPhone应用程序中看到,在状态栏上有一个手势可以访问通知中心。我如何在我的应用程序中实现这种转换呢?我认为这可以通过滑动手势识别器来完成,但是如何包含从上往下的滑动手势(就像你可以拖动通知中心进行完整的转换一样)?有没有示例代码或其他东西可以帮助我做到这一点?
谢谢您的帮助!
谢谢您的帮助!
编辑:
这里是一些示例代码。未经测试,可能包含拼写错误,可能无法编译,但应该包含所需的基本部分。
//... inside mainView impl:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = (UITouch *)[touches anyObject];
start = [touch locationInView:self.superview].y;
if(start > 30 && pulldownView.center.y < 0)//touch was not in upper area of view AND pulldownView not visible
{
start = -1; //start is a CGFloat member of this view
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(start < 0)
{
return;
}
UITouch *touch = (UITouch *)[touches anyObject];
CGFloat now = [touch locationInView:self.superview].y;
CGFloat diff = now - start;
directionUp = diff < 0;//directionUp is a BOOL member of this view
float nuCenterY = pulldownView.center.y + diff;
pulldownView.center = CGPointMake(pulldownView.center.x, nuCenterY);
start = now;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (directionUp)
{
//animate pulldownView out of visibel area
[UIView animateWithDuration:.3 animations:^{pulldownView.center = CGPointMake(pulldownView.center.x, -roundf(pulldownView.bounds.size.height/2.));}];
}
else if(start>=0)
{
//animate pulldownView with top to mainviews top
[UIView animateWithDuration:.3 animations:^{pulldownView.center = CGPointMake(pulldownView.center.x, roundf(pulldownView.bounds.size.height/2.));}];
}
}
UIScrollView
中使用一个子类化的UIView
来解决了这个问题。如果用户触摸到不可拖动的点,UIView
会将触摸事件传递给其下层视图。@implementation MyCustomUIView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
// Check whether we are on the overlay
if
(
point.y < 460.0f
// Maybe some more restrictions here
)
{
// Block the event, pass to parent
return NO;
}
// Everything okay
return YES;
}
@end
这段代码使得 UIScrollView
只在 460 像素以下的触摸事件才会响应。如果你的 UIScrollView
高度为 800 像素,那么你可以从 460 像素到 800 像素之间进行触摸操作。足以打开、关闭和使用它。因此,如果你有一个全尺寸的 UIScrollView
,你可以将其拖动到打开它,同时仍然能够“触摸”底层视图。这种方法的好处是所有动画都依赖于默认行为,而不需要自己实现。