UIView の周りを移動する ImageView がありますが、ImageView とビュー自体の衝突を検出することは可能ですか? たとえば、ImageView がアクションを実行するビューの側面にヒットします。-(void)restart {}
これが可能であれば、衝突した側を検出できますか?
1 に答える
0
カスタム UIImageVIew を作成し、メソッド touchBegan および touchMoved を実装できます (init メソッドを追加することを忘れない[self setUserInteractionEnabled:YES]
でください)。次に、操作する四角形を設定します。
customImageView.interactRect = myView.frame;
また、customImageView に次のようなものを追加できます。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
lastPosition = [touch locationInView: self.superview];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint position = [touch locationInView:self.superview];
CGRect currentFrame = self.frame;
currentFrame.origin = CGPointMake(currentFrame.origin.x + position.x - lastPosition.x, currentFrame.origin.y + position.y - lastPosition.y);
if (CGRectIntersectsRect(currentFrame, interactRect) && !CGRectIntersectsRect(self.frame, interactRect))
{
NSLog(@"I'm in for the first time");
if(self.frame.origin.x + self.frame.size.width <= interactRect.origin.x && currentFrame.origin.x + currentFrame.size.width > interactRect.origin.x)
{
NSLog(@"Coming from the left");
}
if(self.frame.origin.x >= interactRect.origin.x + interactRect.size.width && currentFrame.origin.x < interactRect.origin.x + interactRect.size.width)
{
NSLog(@"Coming from the right");
}
}
self.frame = currentFrame;
lastPosition = position;
}
于 2014-12-03T10:54:54.710 に答える