在UIView IOS中的任意位置按时移动UIImageview并移动触摸
我在在UIView IOS中的任意位置按时移动UIImageview并移动触摸,ios,objective-c,uiview,uiimageview,touchesbegan,Ios,Objective C,Uiview,Uiimageview,Touchesbegan,我在ui视图中有一个ui视图和ui图像视图。我使用触摸方法拖动UIImageView 这很有效。不过我有一个问题 我想在触摸UIView中的任何位置时,拖动或移动UIImageView。目前,只有在触摸UIImageview而非UIView时,UIImageview才能移动 self.scrollView.scrollEnabled = NO; UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(10, 135, 300, 200
ui视图
中有一个ui视图
和ui图像视图
。我使用触摸方法拖动UIImageView
这很有效。不过我有一个问题
我想在触摸UIView中的任何位置时,拖动或移动UIImageView
。目前,只有在触摸UIImageview而非UIView时,UIImageview才能移动
self.scrollView.scrollEnabled = NO;
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(10, 135, 300, 200)];
subview.backgroundColor = [UIColor clearColor];
// Create an instance of the image to drag
ImageToDrag *img2 = [[ImageToDrag alloc] initWithImage:[UIImage imageNamed:@"markit_btn.png"]];
img2.center = CGPointMake(110, 75);
img2.userInteractionEnabled = YES;
[subview addSubview:img2];
[self.view addSubview:subview];
我有一个单独的文件,包含在UIView所在的控制器的头文件中
- (id)initWithImage:(UIImage *)image
{
if (self = [super initWithImage:image])
self.userInteractionEnabled = YES;
return self;
}
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// When a touch starts, get the current location in the view
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
// Get active location upon move
CGPoint activePoint = [[touches anyObject] locationInView:self];
// Determine new point based on where the touch is now located
CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x),
self.center.y + (activePoint.y - currentPoint.y));
//--------------------------------------------------------
// Make sure we stay within the bounds of the parent view
//--------------------------------------------------------
float midPointX = CGRectGetMidX(self.bounds);
// If too far right...
if (newPoint.x > self.superview.bounds.size.width - midPointX)
newPoint.x = self.superview.bounds.size.width - midPointX;
else if (newPoint.x < midPointX) // If too far left...
newPoint.x = midPointX;
float midPointY = CGRectGetMidY(self.bounds);
// If too far down...
if (newPoint.y > self.superview.bounds.size.height - midPointY)
newPoint.y = self.superview.bounds.size.height - midPointY;
else if (newPoint.y < midPointY) // If too far up...
newPoint.y = midPointY;
// Set new center location
self.center = newPoint;
}
-(id)initWithImage:(UIImage*)图像
{
if(self=[super initWithImage:image])
self.userInteractionEnabled=是;
回归自我;
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
//当触摸开始时,获取视图中的当前位置
currentPoint=[[Touchs anyObject]locationInView:self];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
//移动时获得活动位置
CGPoint activePoint=[[toucheanyobject]locationInView:self];
//根据触摸屏现在所在的位置确定新点
CGPoint newPoint=CGPointMake(self.center.x+(activePoint.x-currentPoint.x),
self.center.y+(activePoint.y-currentPoint.y));
//--------------------------------------------------------
//确保我们保持在父视图的范围内
//--------------------------------------------------------
float midPointX=CGRectGetMidX(self.bounds);
//如果太对了。。。
if(newPoint.x>self.superview.bounds.size.width-midPointX)
newPoint.x=self.superview.bounds.size.width-midPointX;
else if(newPoint.xself.superview.bounds.size.height-midPointY)
newPoint.y=self.superview.bounds.size.height-midPointY;
else if(newPoint.y
关于您必须使您的
UIView*子视图
成为一个自定义类,并覆盖touchsbegind:
等等。然后需要使UIImageViewsetUserInteractionEnabled:NO
。这样,自定义UIView
上的任何触摸事件都将由视图控制,您可以使用它或它的子视图(UIImageView)执行任何操作
您的
UIView
子类必须在其类变量中包含您的UIImageView
,以便在触摸事件期间对其进行操作,但这很容易理解。很抱歉,这太晚了,但如果其他人需要此更改方面的帮助,请查看此代码:
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView: touch.view];
"YOURVIEW".center = touchLocation;
基本上,您必须将事件设置为在视图的任何位置发生
我希望这能帮助其他人^ ^在UIView类或UIIamgeView类中,在哪里覆盖ToucheSBegind、touchesMoved?