Iphone 拖动图像视图

Iphone 拖动图像视图,iphone,cocoa-touch,drag-and-drop,uiimageview,Iphone,Cocoa Touch,Drag And Drop,Uiimageview,我正在尝试拖动图像视图。我在这方面取得了一点成功,但它并没有按照我所希望的那样行事。我希望只有在触摸图像内部并拖动它时,它才会移动。 但即使我在屏幕上的任何地方触摸和拖动,它也在移动 我编写了如下代码: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //retrieve touch point CGPoint pt= [[ touches anyObject] loca

我正在尝试拖动图像视图。我在这方面取得了一点成功,但它并没有按照我所希望的那样行事。我希望只有在触摸图像内部并拖动它时,它才会移动。 但即使我在屏幕上的任何地方触摸和拖动,它也在移动

我编写了如下代码:

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
         //retrieve touch point
      CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]];
      startLocation = pt; 

     }

    - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
    {

     CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]];

     CGRect frame = [[self.view.subviews objectAtIndex:0]frame];
     frame.origin.x += pt.x - startLocation.x;
     frame.origin.y += pt.y - startLocation.y;
     [[self.view.subviews objectAtIndex:0] setFrame: frame];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     //retrieve touch point
  startLocation = [[ touches anyObject] locationInView:self.view];
 // Now here check to make sure that start location is within the frame of 
 // your subview [self.view.subviews objectAtIndex:0]
 // if it is you need to have a property like dragging = YES 
 // Then in touches ended you set dragging = NO

 }

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{

 CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]];

 CGRect frame = [[self.view.subviews objectAtIndex:0]frame];
 frame.origin.x += pt.x - startLocation.x;
 frame.origin.y += pt.y - startLocation.y;
 [[self.view.subviews objectAtIndex:0] setFrame: frame];

}

locationInView方法的返回值是相对于图幅的点。首先检查它是否在图幅中

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     CGRect targetFrame = [self.view.subviews objectAtIndex:0].frame;
     //retrieve touch point
     CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]];
     //check if the point in the view frame      
     if (pt.x < 0 || pt.x > targetFrame.size.width || pt.y < 0 || pt.y > targetFrame.size.height)
     {
         isInTargetFrame = NO;
     }
     else
     {
         isInTargetFrame = YES;
         startLocation = pt; 
     }
 }

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
      if(!isInTargetFrame)
      {
          return;
      }
      //move your view here...
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
CGRect targetFrame=[self.view.subviews objectAtIndex:0].frame;
//检索接触点
CGPoint pt=[[touches anyObject]locationInView:[self.view.subviews objectAtIndex:0]];
//检查该点是否位于图幅中
if(pt.x<0 | | pt.x>targetFrame.size.width | | pt.y<0 | | pt.y>targetFrame.size.height)
{
isInTargetFrame=否;
}
其他的
{
isInTargetFrame=是;
位置=pt;
}
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
如果(!isInTargetFrame)
{
返回;
}
//将您的视图移到这里。。。
}

尝试以下方法:

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
         //retrieve touch point
      CGPoint pt= [[ touches anyObject] locationInView:[self.view.subviews objectAtIndex:0]];
      startLocation = pt; 

     }

    - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
    {

     CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]];

     CGRect frame = [[self.view.subviews objectAtIndex:0]frame];
     frame.origin.x += pt.x - startLocation.x;
     frame.origin.y += pt.y - startLocation.y;
     [[self.view.subviews objectAtIndex:0] setFrame: frame];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     //retrieve touch point
  startLocation = [[ touches anyObject] locationInView:self.view];
 // Now here check to make sure that start location is within the frame of 
 // your subview [self.view.subviews objectAtIndex:0]
 // if it is you need to have a property like dragging = YES 
 // Then in touches ended you set dragging = NO

 }

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{

 CGPoint pt = [[touches anyObject] locationInView: [self.view.subviews objectAtIndex:0]];

 CGRect frame = [[self.view.subviews objectAtIndex:0]frame];
 frame.origin.x += pt.x - startLocation.x;
 frame.origin.y += pt.y - startLocation.y;
 [[self.view.subviews objectAtIndex:0] setFrame: frame];

为什么要使用
子视图访问?您可以声明imageview的实例。通过使用
CGRectContainsPoint
检查您是否触摸了imageview。当触摸开始时,您可以检查您的触摸点是否位于imageview中。图像视图的边框是否设置为覆盖整个屏幕?@robmayoff否,我的图像视图比整个屏幕小。正在工作。一些修改:frame不是属性,因此请将其作为消息发送。如果条件允许,请将frame替换为targetFrame。谢谢!