Вопрос

I know there are lots of questions on multi finger touches and restrictions here and there,but none of them seem to work.All they suggest is to try setting exclusive touch and disable multi touch i.e.:

[self.view.subviews makeObjectsPerformSelector:@selector(setExclusiveTouch:) withObject:[NSNumber numberWithBool:YES]];
[self.view setMultipleTouchEnabled:NO];

Note: Initially when I used self.view.exclusiveTouch = NO; ,it didn't work for me.Mr.Hlung's comment in the 1st link provided some useful info of enabling the subviews exclusive touch through selector.

But they seem to work incomplete.I have a game view where there are multiple images on left and their corresponding images on the right.User has to drag the left image to the respective right image and match it.I am using UIPanGestureRecognizer to do so.Here comes a scenario which was the outcome of some rash testing by a tester in my office :)

When the user scrambles the images,i.e. plays awkwardly with all/few of his fingers,what happens is the frames are getting disturbed or few images overlap with one another.This is all because some where multiple touch is still available for user.Hence I want to eliminate that feature.I am also aware of the fact that there is a method called -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event ,through which we can detect the number of user touches.What if I delete all user touches if the count is more than 1,i.e. if more than 1 finger touch is detected!!!

But I found no method to remove touches there,I also tried to assign last touch object to my image view through which I thought would ignore the rest of touches detected,i.e.:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([event allTouches].count>1)
    {
        UITouch *touch = [[touches allObjects] lastObject];
        self.dragObjectImageView = (UIImageView *)[touch view];
    }
}

Even this doesn't seem to work.

EDIT-Setting Max and Min number of touches:

I have also set the pan gesture's max and min no of touches to 1 as well,i.e.:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view.subviews makeObjectsPerformSelector:@selector(setExclusiveTouch:) withObject:[NSNumber numberWithBool:YES]];

       for(UIImageView *iView in self.movableArray){
           if ([iView isMemberOfClass:[UIImageView class]]){
                UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
                recognizer.minimumNumberOfTouches = 1;
                recognizer.maximumNumberOfTouches = 1;
                iView.multipleTouchEnabled = NO;
                [iView addGestureRecognizer:recognizer];
                [iView setUserInteractionEnabled:YES];
                recognizer.delegate = self;
            }
        }
}

Tried another tactic of disabling user interaction for image if multiple touches are detected,but in vain!

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([event allTouches].count > 1)
    {
        for (UIView* view in self.view.subviews)
        {
            if ([view isKindOfClass:[UIImageView class]])
            {
                [view setUserInteractionEnabled:NO];
            }
        }
    }
    else
    {
        for (UIView* view in self.view.subviews)
        {
            if ([view isKindOfClass:[UIImageView class]])
            {
                [view setUserInteractionEnabled:YES];
            }
        }
    }
}

Some people might say there can't be a case where user will use multiple fingers to play.But I was asked to fix the issue and a developer has to obey and respect the testers perspective as well.

Can some one please help me get rid of the problem.

Thanks every one in advance :)

Это было полезно?

Решение

Instead of setting exclusive touch to our own view,turning on the same for the subview or the view we assign to self.view worked,in my case it is...

[gameView.subviews makeObjectsPerformSelector:@selector(setExclusiveTouch:) withObject:[NSNumber numberWithBool:YES]];

Hope it helps some one,thanks :)

Другие советы

As per apple documentation for the property multipleTouchEnabled in UIView Class Reference

multipleTouchEnabled

Other views in the same window can still receive touch events when this property is NO. If you want this view to handle multi-touch events exclusively, set the values of both this property and the exclusiveTouch property to YES.

I see in your code, you've set setExclusiveTouch to all subviews but setMultipleTouchEnabled only for the container view and not all image views. Add the following line before/after setting exclusive touch and single touch may work correctly.

[self.view.subviews makeObjectsPerformSelector:@selector(setMultipleTouchEnabled:) withObject:[NSNumber numberWithBool:NO]];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top