Problem in dragging textField
my problem is that textField does not save the last position so when I dropped my textField he returned to the starting position
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoi开发者_如何转开发nt 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);
}
}
As far as I know, when you use PanGestureRecognizer, you should reset it's translation to 0 every time you use it. So I would recommend you to rewrite this part of code like this:
- (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];
}
}
Another possible reason could be calling of layoutSubviews method of textField superview.
Hope this'll help!
精彩评论