基于页面的应用程序和手势识别器

发布于 2024-12-29 12:27:27 字数 547 浏览 4 评论 0原文

我在 Xcode 4 中为 iPad iOS5 创建了一个基于页面的应用程序。

当我运行该应用程序时,我可以看到书中的页面,并且可以前后翻转它们, 通过点击屏幕或从左到右或从右到左移动手指。

我的问题是,无论我在屏幕、边框的哪个位置按下,页面都会翻页。

我已经设法用以下代码取消用手指翻转:

for (UIGestureRecognizer *gR in self.pageViewController.gestureRecognizers) 
{
    if ([gR isKindOfClass:[UIPanGestureRecognizer class]]) 
    {
        [[gR view] removeGestureRecognizer:gR];
    }
}

如何定义屏幕中的特定区域,当我点击它时,只有它,页面才会翻转?

我问这个是因为我将工具栏放在屏幕底部,当我单击工具栏中的按钮时,页面会翻转。我想在屏幕上放置两个箭头,只有当我按下它们时页面才会翻转。

抱歉,如果我的解释有点生疏。谢谢大家。

I have created a page-based application in Xcode 4, for iPad iOS5.

When I run the app, I can see the pages in the book and can flip them back and forward,
by tap on the screen or by moving the finger from left to right, or right to left.

My problem is that no matter where I'm pressing in the screen, in the borders, the page turns.

I had managed to cancel the flip with fingers with this code:

for (UIGestureRecognizer *gR in self.pageViewController.gestureRecognizers) 
{
    if ([gR isKindOfClass:[UIPanGestureRecognizer class]]) 
    {
        [[gR view] removeGestureRecognizer:gR];
    }
}

How can I define a specific area in the screen that when I tap on it, and only it, the page will turn?

I ask this because I put toolbar in the bottom of the screen and when I click on a button in the toolbar the page flips. I want to put 2 arrows on the screen that only when I press on them the page will flip.

Sorry if my explanation is a little bit rusty. Thank you all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2025-01-05 12:27:27

Cipramill 的答案是正确的——这里有更多细节。

IOS 文档建议添加新的视图来描绘您希望翻页手势处于活动状态的区域,但这种方法要简单得多。在 MQ1RootViewController.h 和 MQ1RootViewController.m 中向 Xcode 4 设置的默认模板添加代码:

更改 MQ1RootViewController.h 中的接口行:

@interface MQ1RootViewController : UIViewController <UIPageViewControllerDelegate,       
    UIGestureRecognizerDelegate>

将此代码添加到 MQ1RootViewController.m 中 viewDidLoad 的最底部:

for (UIGestureRecognizer *gR in self.pageViewController.gestureRecognizers) {
    gR.delegate = self;
}

将此方法添加到 MQ1RootViewController.m:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
  shouldReceiveTouch:(UITouch *)touch
{

if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]
    || [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { 

    CGPoint point = [touch locationInView:self.view];

    if(point.x < 100 || point.x > 924) return YES;

}

return NO;
}

请注意,“滑动”手势实际上是由页面视图控制器对象从“平移”手势派生的。

以上将手势限制在屏幕的左右边缘。这允许使用手势与屏幕中心的对象进行交互,而不会因错误的滑动​​而意外更改页面。

Cipramill's answer is correct -- here are more details.

The IOS documentation suggests adding new Views to delineate the areas where you wish the page turning gestures to be active, but this method is much simpler. Adding code to the default template set up by Xcode 4 in MQ1RootViewController.h and MQ1RootViewController.m:

Change interface line in MQ1RootViewController.h:

@interface MQ1RootViewController : UIViewController <UIPageViewControllerDelegate,       
    UIGestureRecognizerDelegate>

Add this code to the very bottom of viewDidLoad in MQ1RootViewController.m:

for (UIGestureRecognizer *gR in self.pageViewController.gestureRecognizers) {
    gR.delegate = self;
}

Add this method to MQ1RootViewController.m:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
  shouldReceiveTouch:(UITouch *)touch
{

if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]
    || [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { 

    CGPoint point = [touch locationInView:self.view];

    if(point.x < 100 || point.x > 924) return YES;

}

return NO;
}

Note that the "swipe" gesture is actually derived from a "pan" gesture by the page view controller object.

The above limits the gestures to the left and right edges of the screen. This allows gestures to be used to interact with objects in the center of the screen without accidentally changing the page with an errant swipe.

万人眼中万个我 2025-01-05 12:27:27

您可以连接到手势系统并定义接受触摸的区域。

在这个解释中,我假设你的RootViewController有一个UIPageViewController作为子VC:

-设置你的根视图控制器来实现UIGestureRecognizerDelegate

-接管RootViewControllers中pageVC的所有手势识别器ViewDidLoad

for (UIGestureRecognizer *gR in self.pageVC.gestureRecognizers) {
    gR.delegate = self;
}

- 最后在 RootViewController 中实现手势识别器并定义要忽略的区域:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
 if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { 
     CGPoint point = [touch locationInView:self.view];

     //Examine point and return NO, if gesture should be ignored.

   }
   return YES;
}

希望这会有所帮助

you could hook into the the gesture system and define which area to accept touches for.

In this explanation I assume your RootViewController has a UIPageViewController as a child VC:

-Set your root view controller to implement UIGestureRecognizerDelegate

-Take over all gesture recognizers for your pageVC in your RootViewControllers ViewDidLoad:

for (UIGestureRecognizer *gR in self.pageVC.gestureRecognizers) {
    gR.delegate = self;
}

-Finally implement the gesture recognizer in your RootViewController and define which zones you want to ignore:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
 if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { 
     CGPoint point = [touch locationInView:self.view];

     //Examine point and return NO, if gesture should be ignored.

   }
   return YES;
}

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文