我在 iPhone 应用程序中看到状态栏上有一个可以访问通知中心的手势。我如何在我的应用程序中实现这种转换?我认为这是通过滑动手势识别器完成的,但是如何包含从上到下的滑动手势(如何拖动通知中心完成其完整过渡)?有任何示例代码或其他东西可以帮助我做到这一点吗?
提前致谢
应该很容易做到。假设你有一个UIView
(mainView
)你想从中触发下拉事件。
- 放置一个子视图(
pulldownView
) 在 mainView 顶部可见区域之外。
- 实施
touchesBegan
on mainView
并检查触摸是否位于前 30 个像素(或点)内。
- 实施
touchesMoved
在哪里检查移动方向是否向下并且pulldownView
不可见,如果是则拖动pulldownView
向下进入主视图的可见区域或检查移动方向是否向上pulldownView
可见,如果是,则向上推出可见区域。
- 实施
touchesEnd
通过检查拖动或推动的方向来结束拖动或推动移动pulldownView
被感动了。
EDIT:
这是一些示例代码。未经测试,可能包含拼写错误,可能无法编译,但应该包含所需的基本部分。
//... 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.));}];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)