如何拖动和移动UITextFields?

TWc*_*ode 1 objective-c ios4 ios5

我设置了可以移动多个标签的位置.我在移动UITextfield时遇到问题.我确保启用了用户交互和多点触控.

-(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;
Run Code Online (Sandbox Code Playgroud)

ber*_*ium 6

尝试制作这个技巧,它对我有用

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)