iOS - Detecting touches in a UIView?
Asked Answered
A

4

9

So I have a Subclass of UIView that is suppose to detect touches. The view detect touches only if the touches started inside the current view. When the touches start outside of the view and they move inside my custom view touchesMoved doesn't get called. Any solution to detect moving touches that have not started in the current view?

@implementation MycustomView

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   // This only gets called if touches have started in the current View
} 

@end
Ansela answered 1/4, 2012 at 22:54 Comment(2)
This is the documented and intended behavior. Perhaps if you give some insight into what you want to accomplish someone could help you with the how.Rustie
I have multiple custom views on the screen I want to detect the UIViews as the touches move over themAnsela
A
20

The following solution worked. I have multiple instances of MyCustomView; as the touches move I want to detect the views that are being touched

I ended up moving touch detection from MyCustomView to its superView, so the following code is no longer in MyCustomView class:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    for (UIView *view in self.contentView.subviews)
    {
        if ([view isKindOfClass:[MyCustomView class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {

        }
    }
}
Ansela answered 1/4, 2012 at 23:20 Comment(0)
A
1

this should fix it:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    for (UIView* subView in self.subviews) 
    {
        if([subView pointInside:[self convertPoint:touch toView:subView] withEvent:event])
        {
            //do your code here
        }
    }
}
Analogue answered 1/4, 2012 at 23:2 Comment(2)
It behaves the same as what I already have. My touches pass 5 different UIViews, and it always returns the first view that the touches started fromAnsela
The code is exactly what you posted, except I have an NSlog in it: NSLog(@"%@", [touch view]);Ansela
E
0

One way to do it (though there might be others) is to disable user interaction for the subviews and make their parent view track the movement (use the hitTest method to figure out which view the touch is currently over).

Envoi answered 1/4, 2012 at 23:20 Comment(0)
V
0

Try this....

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for(UITouch *touch in touches)
    {
        CGPoint touchPointFirstBtn = [touch locationInView:self.ChordView];
        if(CGRectContainsPoint(_btnC.frame, touchPointFirstBtn))
        {
            if (!_btnC.isHighlighted)
            {
                if(!Boolean)
                {
                    title = @"C";
                    [_tlbView reloadData];
                    NSLog(@"%@",@"touches C");

                }
                [_btnC setHighlighted:YES];
                Boolean = YES;

            }
        }
        else
        {
            [_btnC setHighlighted:NO];
            Boolean = NO;
        }
}
Volleyball answered 31/3, 2017 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.