exclusiveTouch不工作

exclusiveTouch不工作

本文介绍了为什么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.

为了理解属性的工作原理,我创建了一个全新的项目, view controller:

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?

感谢,
Eli

Thanks,Eli

推荐答案

从:

默认情况下,视图的exclusiveTouch属性设置为NO。如果你将
的属性设置为YES,你标记视图,这样,如果它跟踪
touches,它是跟踪触摸的窗口中的唯一视图。
窗口中的其他视图不能接收这些触摸。然而,标记为独占触摸的
视图不接收
与同一窗口中的其他视图相关联的触摸。如果手指
接触到独占触摸视图,则仅当
该视图是跟踪该窗口中的手指的唯一视图时,才传递该触摸。如果
手指触摸非独占视图,则如果在独占触摸视图中没有另一个手指跟踪,则该触摸仅传递

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不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:01