如何移动按钮动作ios

时间:2013-04-11 09:41:35

标签: iphone ios xcode ios5 uibutton

我想在按下按钮时改变按钮的位置,按住时应该一次又一次地向下移动,请告诉我该怎么办?

textField = [[UITextField alloc] initWithFrame:CGRectMake(76, ([txtFieldArray count] * 30), 191, 25)];
[textField setBorderStyle:UITextBorderStyleLine];
textField.font = [UIFont systemFontOfSize:20];
textField.placeholder = @"Enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[myScrollview addSubview:textField];
[txtFieldArray addObject:textField];

CGRect frame = bottomView.frame;
frame.origin.y += textField.frame.size.height + 5;

bottomView.frame = frame;
textField.hidden = NO;

4 个答案:

答案 0 :(得分:0)

在创建UIButton并将其设置为UITextField之后,您应该使用按钮的setFrame:方法来更改它的位置。

如果您想通过按下按钮更改位置,请首先创建-(IBAction *)buttonPressed:(id)sender方法并在其中设置按钮的新位置。然后在创建按钮的位置后添加[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]方法。

答案 1 :(得分:0)

仅在您描述的代码段中提供按钮的框架。

对于前,

<button_object>.frame =
CGRectMake(textField.frame.origin.x,textField.frame.origin.y +
x,textField.size.width,textField.size.height);


Where X = the difference upto which you want to add the button after
textfield. For ex, 44 or 50 or so on..

享受编程!

答案 2 :(得分:0)

您可以将标签指定为按钮

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:100];
[btn addTarget:self action:@selector(moveButtonDown) forControlEvents:UIControlEventTouchUpInside];

每当按下按钮时,可以通过获取按钮的参考来更改按钮的框架。在下面的代码中,我将按钮向下移动了20个像素。您可以将此值更改为您想要的值。

 -(void)moveButtonDown
{
    UIButton *btn=(UIButton *)[self.view viewWithTag:100];
    float y=btn.frame.origin.y;
    y+=20;
    [btn setFrame:CGRectMake(btn.frame.origin.x, y, btn.frame.size.width, btn.frame.size.height)];
}

答案 3 :(得分:0)

您必须通过以下方式修改代码:

CGRect frame = bottomView.frame;
frame.origin.y += (tField.frame.origin.y+textField.frame.size.height + 5);

bottomView.frame = frame;
textField.hidden = NO;