1

我在屏幕底部有一个图像。

我希望用户向上移动图像,直到它到达某个 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)];
}
4

1 回答 1

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); 
    } 
}  
于 2012-10-28T18:55:31.707 回答