0
我有一個ImageView,圍繞UIView移動,是否有可能檢測ImageView和視圖的自我碰撞?例如,ImageView打到我希望它運行一個動作的視圖的一側。 -(void)restart {}
如果這是可能的,你可以檢測到它與哪邊碰撞?在UIView的邊界上使用碰撞
我有一個ImageView,圍繞UIView移動,是否有可能檢測ImageView和視圖的自我碰撞?例如,ImageView打到我希望它運行一個動作的視圖的一側。 -(void)restart {}
如果這是可能的,你可以檢測到它與哪邊碰撞?在UIView的邊界上使用碰撞
您可以創建自定義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;
}
這工作,但我只是想它,如果它是從側面可以請你幫忙接着就,隨即。 – Ajay 2014-12-03 11:08:23
我更新了我的答案。 – 2014-12-03 11:30:13
thx這爲我工作。 – Ajay 2014-12-05 05:53:00