問題描述
我在我的 iPhone 應用中看到狀態欄上有一個手勢可以訪問通知中心.如何在我的應用程序中實現這種轉換?我認為這是通過滑動手勢識別器完成的,但是如何包含從上到下的滑動手勢(如何將通知中心拖動到其完整過渡)?是否有任何示例代碼或可以幫助我做到這一點的東西?提前謝謝
I saw in my iPhone app that there is a gesture on the status bar which can access Notification Center. How can I implement that kind of transition in my app?. I think this is done with the swipe gesture recognizer, but how do I include a swipe gesture from top to bottom (how you can drag the Notification Center through its full transition)? Is there any sample code or something that can help me do this? Thaks in advance
推薦答案
應該很容易做到.假設您有一個 UIView
(mainView
),您想從中觸發下拉操作.
Should be easy to do. Let's say you have a UIView
(mainView
) from which you want to trigger the pull down thing.
- 在 mainView 可見區域的頂部外部放置一個子視圖(
pulldownView
). - 在
mainView
上實現touchesBegan
并檢查觸摸是否在前 30 個像素(或點)中. - 在你檢查的地方實現
touchesMoved
,如果移動方向向下并且pulldownView
不可見,如果是這樣,將pulldownView
向下拖動到可見區域主視圖或檢查移動方向是否向上并且pulldownView
可見,如果是,則向上推出可見區域. - 實現
touchesEnd
,通過檢查pulldownView
的移動方向來結束拖動或推動移動.
- Put a subview (
pulldownView
) on mainView top-outside of visible area. - Implement
touchesBegan
onmainView
and check if the touch is in the top 30 pixels (or points). - Implement
touchesMoved
where you check, if move direction is down andpulldownView
not visible and if so drag thepulldownView
down into visible area of main view or check if move direction is up andpulldownView
visible and if so push upwards out of visible area. - Implement
touchesEnd
where you end the drag or push movement by checking in which direction thepulldownView
was moved.
這里有一些示例代碼.未經測試,可能包含拼寫錯誤,可能無法編譯,但應該包含所需的基本部分.
Here's some sample code. Untested, may contain typos, maybe won't compile, but should contain the essential part needed.
//... 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.));}];
}
}
這篇關于在 iOS 5 中下拉 UIView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!