触摸 - 如何在达到某个Y点时阻止UIIMageView移动

时间:2012-10-28 18:27:36

标签: iphone ios

我在屏幕底部有一张图片。

我希望用户向上移动图像,直到达到某个Y点,如下所示:

[door setCenter:CGPointMake(160,347)];

到目前为止,当您向上拖动图像(门)时,它会继续经过我的目标点,但是当你放开它时,它会快速回到正确的位置。

如果用户的手指仍在向上滑动,如何在到达某一点时停止移动图像?它会在if语句中吗?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (startPoint.y < 347) {
         // something in here ?????
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *myTouch = [touches anyObject];
    startPoint = [myTouch locationInView:self.view];
    NSLog(@"position = %f and  %f",startPoint.x,startPoint.y);
    [door setCenter:CGPointMake(160, startPoint.y)];
}  

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [door setCenter:CGPointMake(160,347)];
}

1 个答案:

答案 0 :(得分:1)

如何在touchesMoved方法中设置它。像,

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *myTouch = [touches anyObject];
    startPoint = [myTouch locationInView:self.view];
    NSLog(@"position = %f and  %f",startPoint.x,startPoint.y);
    if (startPoint.y < 347) { //or suitable condition to verify your case
        [door setCenter:CGPointMake(160, startPoint.y)]; //then only set the center
    } else 
    { 
       [door setCenter:CGPointMake(160,347); 
    } 
}