TWcode
TWcode

Reputation: 823

How to drag and move UITextFields?

I have it set up where i can move multiple labels. I'm having problems moving a UITextfield. I made sure that user interaction and multiple touch is enabled.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 UITouch *touch = [[event allTouches] anyObject];
 CGPoint location = [touch locationInView:self.view];

 if ([touch view] == tex1) {
    tex1.center = location;

Upvotes: 1

Views: 1968

Answers (1)

beryllium
beryllium

Reputation: 29767

Try to make this trick, it works for me

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *clearView = [[UIView alloc] initWithFrame:tex1.frame];
    [clearView setTag:101];
    [self.view addSubview:clearView];
    [clearView release];
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];

    if ([[touch view] tag] == 101 && [touch tapCount] == 2) {
        [tex1 becomeFirstResponder];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.view];

    if ([[touch view] tag] == 101) {
        tex1.center = location;
        [[touch view] setCenter:location];
    }
}

Upvotes: 6

Related Questions