Ios 在UIView的边界上使用冲突
我有一个围绕UIView移动的ImageView,是否可以检测ImageView和视图本身的冲突?例如,ImageView点击视图的一侧,我希望它运行一个操作<代码>-(无效)重新启动{}Ios 在UIView的边界上使用冲突,ios,objective-c,cocoa-touch,uiimageview,cgrectintersectsrect,Ios,Objective C,Cocoa Touch,Uiimageview,Cgrectintersectsrect,我有一个围绕UIView移动的ImageView,是否可以检测ImageView和视图本身的冲突?例如,ImageView点击视图的一侧,我希望它运行一个操作-(无效)重新启动{} 如果这是可能的,您是否可以检测它与哪一侧发生冲突?您可以创建一个自定义UIImageVIew并实现touchBegind和touchMoved方法(不要忘记在init方法中添加[self-setUserInteractionEnabled:YES])。然后设置要与之交互的rect: customImageView.i
如果这是可能的,您是否可以检测它与哪一侧发生冲突?您可以创建一个自定义UIImageVIew并实现touchBegind和touchMoved方法(不要忘记在init方法中添加
[self-setUserInteractionEnabled:YES]
)。然后设置要与之交互的rect:
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;
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event{
UITouch*touch=[[event AllTouchs]anyObject];
lastPosition=[触摸位置视图:self.superview];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
UITouch*touch=[[event AllTouchs]anyObject];
CGPoint位置=[触摸位置视图: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,interactirect)和&!CGRectIntersectsRect(self.frame,interactirect))
{
NSLog(@“我第一次参加”);
if(self.frame.origin.x+self.frame.size.width interactirect.origin.x)
{
NSLog(@“来自左边”);
}
if(self.frame.origin.x>=interactirect.origin.x+interactirect.size.width&¤tFrame.origin.x
这可以用,但我想要它,如果它是从侧面来的,请你帮忙。