UIView
私は私のメインビューにたくさんあります。touchesBegan、touchesMoved、touchesEndedの機能をアプリに追加しました。
移動すると、タッチがビューの外にない限り、そのポイントのビューが複数回表示されますCGRect
。ビュー上を移動するときに各ビューを1回だけ参照し、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
ダンプは同じビューを複数回ダンプします。ビュー上に移動したときに、ビューから再び移動するまで1回だけに制限したいと思います。
助言がありますか?