0

我的UIView主要观点有很多。我在应用程序中添加了 touchesBegan、touchesMoved 和 touchesEnded 功能。

移动时,该点的视图会显示多次,除非触摸超出视图的CGRect. 我只想在移过视图时引用每个视图一次,除非移出 CGRect 然后再移入,否则不予考虑。这是我的代码:

-(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;
}

因此,NSLog多次转储相同的视图。我想在移到视图上时将其限制为仅一次,直到再次移出视图为止。

有什么建议么?

4

1 回答 1

1

跟踪触摸进入的最后一个视图,然后您可以优化您的功能以检查触摸是否仍在该视图中,然后再检查其他视图

于 2012-07-20T19:15:17.803 回答