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