我在我的iphone应用程序中看到,状态栏上有一个可以访问通知中心的手势。如何在我的应用程序中实现这种转换?。我认为这是通过滑动手势识别器完成的,但如何包括从上到下的滑动手势(如何拖动通知中心完成完全转换)?有什么样的代码或其他东西可以帮助我做到这一点吗?
提前到达
最佳答案
应该很容易做到。假设您有一个UIView
(mainView
)想要从中触发下拉事件。
将子视图(pulldownView
)放在主视图顶部可见区域之外。
在touchesBegan
上执行mainView
并检查触摸是否位于前30个像素(或点)。
在您检查的位置执行touchesMoved
,如果移动方向是向下且pulldownView
不可见,如果是,则将pulldownView
向下拖动到主视图的可见区域,或者检查移动方向是向上且pulldownView
可见,如果是,则向上推出可见区域。
执行touchesEnd
通过检查pulldownView
移动的方向来结束拖动或推动移动。
编辑:
这是一些示例代码。未经测试,可能包含拼写错误,可能不会编译,但应包含所需的基本部分。
//... 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.));}];
}
}