頂部和底部邊緣手勢從屏幕邊緣觸發20像素。
默認情況下,狀態欄是可見的,並佔據前20像素的空間,因此您必須隱藏狀態欄以使用這些(頂部/機器人)UIScreenEdgePanGestureRecognizers,否則委託不會被調用。
第一步:
步驟2:
添加以下代碼:
ViewController.h
@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIScreenEdgePanGestureRecognizer *topEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleTopEdgeGesture:)];
topEdgeGesture.edges = UIRectEdgeTop; //UIRectEdgeBottom
topEdgeGesture.delegate = self;
[self.view addGestureRecognizer:topEdgeGesture];
}
- (BOOL) prefersStatusBarHidden
{
return YES;
}
- (void)handleTopEdgeGesture:(UIScreenEdgePanGestureRecognizer *)gesture
{
NSLog(@"TOP");
}
斯威夫特:
class ViewController: UIViewController,UIGestureRecognizerDelegate {
override func viewDidLoad()
{
super.viewDidLoad()
var edgeGesture : UIScreenEdgePanGestureRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action:"handleTopEdgeGesture:")
edgeGesture.edges = UIRectEdge.Top
edgeGesture.delegate = self
self.view.addGestureRecognizer(edgeGesture)
}
func handleTopEdgeGesture(gesture:UIScreenEdgePanGestureRecognizer)
{
println("TOP")
}
override func prefersStatusBarHidden() -> Bool
{
return true
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
謝謝,令人驚訝的是同時適用於邊緣。我不完全滿意,因爲我必須擺脫狀態欄。但如果是唯一的方法,我該怎麼做...... – 2014-09-04 10:06:33
如果沒有人提出任何更好的建議,我會在幾天內接受此答案。 – 2014-09-04 10:07:46
如果你真的想擁有狀態欄,我相信你必須做你自己的觸摸事件跟蹤系統(使用touchBegan等)。 – Pierre 2014-09-04 12:05:08