如何拖动和移动UITextFields?

时间:2012-02-08 19:17:30

标签: 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;

1 个答案:

答案 0 :(得分: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];
    }
}