peace4theapes
peace4theapes

Reputation: 171

iOS - touchesmoved only once per view

I have a bunch of UIView on my main view. I have added the touchesBegan, touchesMoved and touchesEnded functionality to the app.

When moved, the view at the point is being shown multiple times, unless the touch is out of the view's CGRect. I would like to refer to each view only once when move over the view and not be considered unless moved out of the CGRect and then back in. Here's my code:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    UITouch *myTouch = [touches anyObject];

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

    UIView *movedView = [self viewAtPoint:point];

    if(movedView != nil)
    {
    NSLog(@"Touches Moved - %@",movedView.name);
    }
}


-(UIView *) viewAtPoint:(CGPoint) point
{   
CGRect touchRect = CGRectMake(point.x, point.y, 1.0, 1.0);

    for( UIView *c in viewArray.views ){
        if( CGRectIntersectsRect(c.frame, touchRect) ){
            return c; 
        }       
    }
    return nil;
}

So the NSLog dumps the same view multiple times. I want to limit it to only once when moved over the view until moved out of the view again.

Any suggestions?

Upvotes: 0

Views: 484

Answers (1)

Dan F
Dan F

Reputation: 17732

Keep track of the last view that the touch moved into, and then you can optimize your function to check if the touch is still in that view first, before checking against others

Upvotes: 1

Related Questions