为什么 UIView.exclusiveTouch 不起作用? [英] Why doesn't UIView.exclusiveTouch work?

查看:19
本文介绍了为什么 UIView.exclusiveTouch 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个 iPhone 项目中,我有三个视图,您可以通过触摸和拖动来四处移动.但是,我想通过使用两个手指来阻止用户同时移动两个视图.因此,我尝试使用 UIView.exclusiveTouch 进​​行试验,但没有成功.

In one of my iPhone projects, I have three views that you can move around by touching and dragging. However, I want to stop the user from moving two views at the same time, by using two fingers. I have therefore tried to experiment with UIView.exclusiveTouch, without any success.

为了了解属性的工作原理,我创建了一个全新的项目,在视图控制器中使用以下代码:

To understand how the property works, I created a brand new project, with the following code in the view controller:

- (void)loadView {

    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    UIButton* a = [UIButton buttonWithType:UIButtonTypeInfoDark];
    [a addTarget:self action:@selector(hej:) forControlEvents:UIControlEventTouchUpInside];
    a.center = CGPointMake(50, 50);
    a.multipleTouchEnabled = YES;

    UIButton* b = [UIButton buttonWithType:UIButtonTypeInfoDark];
    [b addTarget:self action:@selector(hej:) forControlEvents:UIControlEventTouchUpInside];
    b.center = CGPointMake(200, 50);
    b.multipleTouchEnabled = YES;

    a.exclusiveTouch = YES;

    [self.view addSubview:a];
    [self.view addSubview:b];

}

- (void)hej:(id)sender
{
    NSLog(@"hej: %@", sender);
}

当运行这个时,hej: 被调用,不同的发件人,当按下任何按钮时 - 即使其中一个按钮的 exclusiveTouch 设置为 YES.我试过评论 multipleTouchEnabled 行,但无济于事.有人可以向我解释我在这里缺少什么吗?

When running this, hej: gets called, with different senders, when pressing any of the buttons - even though one of them has exclusiveTouch set to YES. I've tried commenting the multipleTouchEnabled-lines, to no avail. Can somebody explain to me what I'm missing here?

谢谢,以利

推荐答案

来自 iPhone 操作系统编程指南:

将事件传递限制为单个视图:

Restricting event delivery to a single view:

默认情况下,视图的exclusiveTouch 属性设置为NO.如果你设置属性为 YES,您标记视图以便,如果它正在跟踪触摸,它是窗口中唯一跟踪触摸的视图.窗口中的其他视图无法接收这些触摸.然而,一个标记为独占触摸"的视图不会接收到与同一窗口中的其他视图相关联.如果一根手指接触一个独占触摸视图,那么该触摸仅在以下情况下传递该视图是该窗口中唯一跟踪手指的视图.如果一个手指触摸非排他性视图,然后仅传递该触摸如果在独占触摸视图中没有其他手指跟踪.

By default, a view’s exclusiveTouch property is set to NO. If you set the property to YES, you mark the view so that, if it is tracking touches, it is the only view in the window that is tracking touches. Other views in the window cannot receive those touches. However, a view that is marked "exclusive touch" does not receive touches that are associated with other views in the same window. If a finger contacts an exclusive-touch view, then that touch is delivered only if that view is the only view tracking a finger in that window. If a finger touches a non-exclusive view, then that touch is delivered only if there is not another finger tracking in an exclusive-touch view.

它指出独占触摸属性不会影响视图框架外的触摸.

It states that the exclusive touch property does NOT affect touches outside the frame of the view.

过去为了处理这个问题,我使用主视图来跟踪屏幕上的所有触摸,而不是让每个子视图跟踪触摸.最好的办法是:

To handle this in the past, I use the main view to track ALL TOUCHES on screen instead of letting each subview track touches. The best way is to do:

if(CGRectContainsPoint(thesubviewIcareAbout.frame, theLocationOfTheTouch)){
    //the subview has been touched, do what you want
}

这篇关于为什么 UIView.exclusiveTouch 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆