繁体   English   中英

拖动textField时出现问题

[英]Problem in dragging textField

我的问题是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);  
    }
}

据我所知,当您使用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方法。

希望这会有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM