我可以使用 UIScrollView 以两种不同的方式移动两个不同的对象吗?Xcode

Can i move two different objects in two different moves with UIScrollView ?Xcode

我进入了 UIScrollView 两个对象,一个 Button 和一个 textField.When 我按下 textField,我需要按钮向上移动,而 textField 向下移动。 我该怎么做?

将 UIButton 保持在 UITextField 下方,或者您可以为 Y 位置指定一些特定值。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
 {
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:1.0];
      //you can change the setAnimationDuration value, it is in seconds.
      CGFloat textFieldY = textField.frame.origin.y;
      CGFloat buttonY = button.frame.origin.y;
      CGRect textRect = CGRectMake(textField.frame.origin.x, buttonY, textField.frame.size.height, textField.frame.size.width);
      CGRect buttonRect = CGRectMake(button.frame.origin.x, textFieldY, button.frame.size.height, button.frame.size.width);
      [textField setFrame:textRect];
      [button setFrame:buttonRect];
      [UIView commitAnimations];
      return YES;
 }

中相同
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:1.0];
      //you can change the setAnimationDuration value, it is in seconds.
      CGFloat textFieldY = textField.frame.origin.y;
      CGFloat buttonY = button.frame.origin.y;
      CGRect textRect = CGRectMake(textField.frame.origin.x, buttonY, textField.frame.size.height, textField.frame.size.width);
      CGRect buttonRect = CGRectMake(button.frame.origin.x, textFieldY, button.frame.size.height, button.frame.size.width);
      [textField setFrame:textRect];
      [button setFrame:buttonRect];
      [UIView commitAnimations];
      return YES;
 }

您可以使用按钮插座代替 "button"。

注意 - 我还没有测试过这段代码。希望有用。