如何使textField移动

时间:2011-03-24 14:06:52

标签: iphone objective-c

当我按下它时,我可以在View中自由改变位置

- (IBAction)add:(id)sender {

CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);

UITextField * textfieldToAdd = [[[UITextField alloc] initWithFrame:frame] autorelease];






        textfieldToAdd.borderStyle =  UITextBorderStyleRoundedRect; 
        textfieldToAdd.textColor = [UIColor blackColor];

        textfieldToAdd.font = [UIFont systemFontOfSize:17.0];
        textfieldToAdd.placeholder = @"";
        textfieldToAdd.backgroundColor = [UIColor whiteColor];
        textfieldToAdd.autocorrectionType = UITextAutocorrectionTypeNo ; // no auto correction support

        textfieldToAdd.keyboardType = UIKeyboardTypeDefault;   // use the default type input method (entire keyboard)
        textfieldToAdd.returnKeyType = UIReturnKeyDone;

        textfieldToAdd.clearButtonMode = UITextFieldViewModeWhileEditing;  // has a clear 'x' button to the right

        textfieldToAdd.tag = kViewTag;     // tag this control so we can remove it later for recycled cells

        textfieldToAdd.delegate = self;    // let us be the delegate so we know when the keyboard's "Done" button is pressed

        // Add an accessibility label that describes what the text field is for.
        [textfieldToAdd setAccessibilityLabel:NSLocalizedString(@"textfieldToAdd", @"")];



[self.view addSubview:textfieldToAdd];

}

2 个答案:

答案 0 :(得分:1)

首先将gestureRecognizer添加到ViewDidLoad,然后创建函数
或者更好看这里MoveME example

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
[panGesture setMaximumNumberOfTouches:2];
[panGesture setDelegate:self];
[self addGestureRecognizer:panGesture];

- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer     state] == UIGestureRecognizerStateChanged) {
        CGPoint translation = [gestureRecognizer translationInView:self.view];
        textfieldToAdd.center = CGPointMake([self center].x + translation.x, [self center].y + translation.y);
        [gestureRecognizer setTranslation:CGPointZero inView:[self superview]];
    }
}

答案 1 :(得分:0)

我要拖动此textField

  • (void)viewDidLoad {

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add :)] autorelease];

}

- (IBAction)add:(id)sender {

CGRect frame = CGRectMake(kLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);

UITextField * textfieldToAdd = [[[UITextField alloc] initWithFrame:frame] autorelease];






        textfieldToAdd.borderStyle =  UITextBorderStyleRoundedRect; 
        textfieldToAdd.textColor = [UIColor blackColor];

        textfieldToAdd.font = [UIFont systemFontOfSize:17.0];
        textfieldToAdd.placeholder = @"";
        textfieldToAdd.backgroundColor = [UIColor whiteColor];
        textfieldToAdd.autocorrectionType = UITextAutocorrectionTypeNo ; // no auto correction support

        textfieldToAdd.keyboardType = UIKeyboardTypeDefault;   // use the default type input method (entire keyboard)
        textfieldToAdd.returnKeyType = UIReturnKeyDone;

        textfieldToAdd.clearButtonMode = UITextFieldViewModeWhileEditing;  // has a clear 'x' button to the right

        textfieldToAdd.tag = kViewTag;     // tag this control so we can remove it later for recycled cells
        textfieldToAdd.delegate = self;    // let us be the delegate so we know when the keyboard's "Done" button is pressed

        // Add an accessibility label that describes what the text field is for.
        [textfieldToAdd setAccessibilityLabel:NSLocalizedString(@"textfieldToAdd", @"")];





[self.view addSubview:textfieldToAdd];

}