【问题标题】:With touches - how to stop UIIMageView moving when reaching a certain Y point触摸 - 如何在到达某个 Y 点时停止 UIIMageView 移动
【发布时间】:2012-11-04 08:24:19
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios


    【解决方案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); 
        } 
    }  
    

    【讨论】:

    • 谢谢,但这不起作用 - 当您滑动时,图像仍会移动到屏幕顶部,直到 touchEnded。还有什么想法吗??
    • 在if块后面加else { [door setCenter:CGPointMake(160,347); }怎么样?
    • @barley,谢谢。我完全错过了那部分。皮特,我也用其他部分更新了我的答案。检查一下。
    • @ACB 和 Barley - 刚刚添加了 else 就可以了!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2018-04-05
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多