我的问题是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);
}
}
答案 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方法。
希望这会有所帮助!