是否可以使 UIScreenEdgePanGestureRecognizer 处理来自顶部(或底部)边缘的事件?
UIScreenEdgePanGestureRecognizer *gestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(userDidPan:)];
gestureRecognizer.edges = UIRectEdgeTop; // UIRectEdgeBottom
[self.view addGestureRecognizer:gestureRecognizer];
问候。
答案 0 :(得分:3)
顶部和底部边缘手势从屏幕边缘触发20像素。
默认情况下,状态栏为Visible,占据前20个像素的空间,因此您必须隐藏状态栏才能使用这些(top / bot)UIScreenEdgePanGestureRecognizers,否则代理不会被调用。
第1步:
第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.
}
}
答案 1 :(得分:2)
状态栏必须隐藏才能正常工作。在UIViewController
中,覆盖
(BOOL)prefersStatusBarHidden()
并返回YES。
然而在ios8中(没有在ios7中测试)。我有一些问题,当设备方向为横向左侧时未识别到底部手势(当设备方向为横向右侧时无法识别顶部手势)。我正在调查,但对我来说看起来像个错误。