拖动textField时出现问题

时间:2011-04-06 10:50:35

标签: iphone objective-c

我的问题是textField没有保存最后一个位置所以当我删除textField时他返回到起始位置

- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer     state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gestureRecognizer translationInView:self.view];

        //textfieldToAdd.center = CGPointMake([textfieldToAdd center].x + translation.x, [textfieldToAdd center].y + translation.y);
        textfieldToAdd.center = CGPointMake(translation.x, translation.y);  
    }
}

1 个答案:

答案 0 :(得分:1)

据我所知,当你使用PanGestureRecognizer时,你应该在每次使用它时将它的翻译重置为0。所以我建议你重写这部分代码:

- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer     state] == UIGestureRecognizerStateChanged) 
    {
        CGPoint translation = [gestureRecognizer translationInView:self.view];
        CGPoint textFieldCenter = textfieldToAdd.center;

        textfieldToAddCenter.x += translation.x;
        textfieldToAddCenter.y += translation.y;
        textfieldToAdd.center = textfieldCenter;

        [gestureRecognizer setTranslation:CGPointZero inView:self.view];
    }
}

另一个可能的原因可能是调用textField superview的layoutSubviews方法。

希望这会有所帮助!