Reputation: 171
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
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