IOS:只在一个方向拖动uiimageview

时间:2012-03-01 16:54:58

标签: ios xcode drag

我想只从水平点1到点2拖动uiimageview;我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

你可以使用这样的东西:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      touchStart = [[touches anyObject] locationInView:/*your main view*/];
        point1 = CGPointMake(140, 140);
        oldrect = imgview.rect;

}

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
      CGPoint point = [[touches anyObject] locationInView:/*your main view*/];
      imgview.center = CGPointMake(imgview.center.x + point.x - touchStart.x, imgview.center.y);
    }

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        CGPoint point = [[touches anyObject] locationInView:/*your main view*/];

    [UIView animateWithDuration:0.25
                         animations:^{

    if (((point1.x -15) > point.x) && ((point1.x + 15 ) < point.x) {
            Imgview.rect = CGRectMake(point.x, point.x, imgview.frame.size.width,imgview.frame.size.height);
        }
    else {Imgview.rect = oldrect;}
                         completion:^(BOOL finished){ }];}  }

答案 1 :(得分:0)

简单,检查你的imageView x位置和你的触摸x位置

float lastX = myimageview.center.x;

如果触摸点x大于lastX(或更小,取决于您想要的方向) {  //做一下 } else {  // 没做什么 }

您的代码看起来应该是这样的:

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    // Move relative to the original touch point
    CGPoint pt = [[touches anyObject] locationInView:self];
    float lastX = myimageview.center.x;
    if (pt.x > lastX){
       // move myimageview.x to pt.x
    }
}