Ketan Shinde
Ketan Shinde

Reputation: 1847

moving UITextField in UIView

i am using UITextField on my UIView and need to drag UITextField with my pointer on my UIView but its not working!! here is my code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[UIView beginAnimations:@"Move" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationBeginsFromCurrentState:YES];
UITouch *touch = [[event allTouches] anyObject];
mytextField.center = [touch locationInView:myView];
[UIView commitAnimations]; }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
mytextField.center = [touch locationInView:myView];}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

Upvotes: 1

Views: 516

Answers (2)

TheTiger
TheTiger

Reputation: 13364

You wanna move to UITextField with touches then use this code -

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint pt = [[touches anyObject] locationInView:textFieldObj];

    // textFieldObj is your UITextfield 
    startLocationOfText = pt;

    //startLocationOfText is just CGPoint declared globaly
}

now touches moved method

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint pt = [[touches anyObject] previousLocationInView:textField2];
    CGFloat dx = pt.x - startLocationOfText.x;
    CGFloat dy = pt.y - startLocationOfText.y;
    CGPoint newCenter = CGPointMake(textFieldObj.center.x + dx, textFieldObj.center.y + dy);
    textFieldObj.center = newCenter;
}

You can comments the touchesBegan method but for clear move you should keep this method.

Thank you!

Upvotes: 2

Iducool
Iducool

Reputation: 3661

Why you have taken animation in touches began.I think just comment code of touches began and try it will work.

Upvotes: 0

Related Questions