Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 发生触摸事件时UIImageView跳转_Ios_Objective C_Uiimageview_Touchesmoved - Fatal编程技术网

Ios 发生触摸事件时UIImageView跳转

Ios 发生触摸事件时UIImageView跳转,ios,objective-c,uiimageview,touchesmoved,Ios,Objective C,Uiimageview,Touchesmoved,好的,基本上这段代码现在做的是根据用户拖动图像的位置沿Y轴上下拖动图像,然后图像返回到原始位置。我的问题是,如果有人不直接触摸UIImageView的中心并开始拖动,它会摇晃(非常不平滑)。当有人触摸UIImageView并开始拖动UIImageView时,UIImageView会稍微晃动,以直接移动到触摸事件的中心 我在考虑使用动画只是为了将其移动到图像需要移动的地方,还是有其他方法 如果这是一种低效的方式,我很抱歉。我对IOS的世界相当陌生 以下是我所拥有的: -(void)touchesB

好的,基本上这段代码现在做的是根据用户拖动图像的位置沿Y轴上下拖动图像,然后图像返回到原始位置。我的问题是,如果有人不直接触摸UIImageView的中心并开始拖动,它会摇晃(非常不平滑)。当有人触摸UIImageView并开始拖动UIImageView时,UIImageView会稍微晃动,以直接移动到触摸事件的中心

我在考虑使用动画只是为了将其移动到图像需要移动的地方,还是有其他方法

如果这是一种低效的方式,我很抱歉。我对IOS的世界相当陌生

以下是我所拥有的:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //Gets location of UIImageView.
    self.originalFrame = self.foregroundImage.frame;
}
//This method is used for moving the UIImageView along the y axis depending on touch events.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if([touch view]==self.foregroundImage) {
        CGPoint location = [touch locationInView:self.view];
        location.x=self.foregroundImage.center.x;
        self.foregroundImage.center=location;
    }
}
//This method sets the UIImageView back to its original position.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    CGRect newFrame = self.foregroundImage.frame;
    newFrame.origin.y = self.originalFrame.origin.y;
    [UIView animateWithDuration:1.1 animations:^{
        self.foregroundImage.frame = newFrame;
    }];
}

您还需要保存ToucheSStart中相对于父视图的第一个位置。然后,您可以使用该值通过上一个位置和新位置之间的差异来更改帧。请参阅以下代码

- (void) touchesBegan: (NSSet*)   touches
            withEvent: (UIEvent*) event
{
  if (touches.count == 1)
  {
    UITouch* touch = [touches anyObject];
    self.touchLocation = [touch locationInView: self.view];
  }
}

- (void) touchesMoved: (NSSet*)   touches
            withEvent: (UIEvent*) event
{
  if (touches.count == 1)
  {
    UITouch* touch = [touches anyObject];
    CGPoint newTouchLocation = [touch locationInView: self.view];

    if (touch.view == self.foregroundImage)
    {
      /* Determine the difference between the last touch locations */
      CGFloat deltaX = newTouchLocation.x - self.touchLocation.x;
      CGFloat deltaY = newTouchLocation.y - self.touchLocation.y;

      /* Offset the foreground image */
      self.foregroundImage.center
        = CGPointMake(self.foregroundImage.center.x + deltaX,
                      self.foregroundImage.center.y + deltaY);
    }

    /* Keep track of the new touch location */
    self.touchLocation = newTouchLocation;
  }
}

非常感谢你!我现在可以看到了。这正是我想要的。再次感谢你!