0

我想在单击 TextField 时移动视图,但它仅在textDidChange我们更改文本或编写任何内容时才有效,因此当我们单击 TextField 时如何调用方法。

 -(void)textFieldTextDidChange:(UITextField*)tf{

    [self  showAnimationPests];


}


    -(void) showAnimationPests{


      [UIView animateWithDuration:0.5 
                 animations:^{
                     self.view.frame = CGRectMake(0,-300,1024,768);
                 }];

     }

而不是我想要在文本字段上单击的文本更改

在 .h 文件中

    IBOutlet UIButton*button1;
IBOutlet UIButton*button2;
IBOutlet UIButton*button3;
IBOutlet UIButton*button4;
IBOutlet UIButton*button5;
IBOutlet UIButton*button6;
IBOutlet UIButton*button7;
IBOutlet UIButton*button8;
IBOutlet UIButton*button9;
IBOutlet UIButton*button10;

    @property(nonatomic,retain)IBOutlet UIButton*button1;
    @property(nonatomic,retain)IBOutlet UIButton*button2;
    @property(nonatomic,retain)IBOutlet UIButton*button3;
    @property(nonatomic,retain)IBOutlet UIButton*button4;
    @property(nonatomic,retain)IBOutlet UIButton*button5;
    @property(nonatomic,retain)IBOutlet UIButton*button6;
    @property(nonatomic,retain)IBOutlet UIButton*button7;
    @property(nonatomic,retain)IBOutlet UIButton*button8;
    @property(nonatomic,retain)IBOutlet UIButton*button9;
    @property(nonatomic,retain)IBOutlet UIButton*button10;
4

4 回答 4

2

采用

- (void)textFieldDidBeginEditing:(UITextField *)textField

委托方法。当UITextField成为第一响应者时调用它。在您单击文本字段的那一刻,就会调用此方法。

于 2013-02-04T06:46:37.847 回答
1

您可以使用委托方法来做到这一点

- (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        [self  showAnimationPests];
    }

或将点击手势添加到您的文本字段

- (void)viewDidLoad
    {
        [super viewDidLoad];

        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];
        [singleTap setNumberOfTapsRequired:1];

        [tf addGestureRecognizer:singleTap];
    }

- (void)handleSingleTap
    {
        [self  showAnimationPests];
    }
于 2013-02-04T06:50:15.700 回答
1

添加此代码:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slideUpView:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slideDownView:) name:UIKeyboardWillHideNotification object:nil];

    -(void)slideDownView:(NSNotification*)notification
{

    [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:animationCurve
                     animations:^{
                         self.view.frame = CGRectMake(0, 0, 320, 480);
                     }
                     completion:^(BOOL finished){
                         NSLog(@"Slide down Done..!");
                     }];
}

-(void)slideUpView:(NSNotification*)notification
{

    [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];
    //
    [UIView animateWithDuration:animationDuration
                          delay:0.0
                        options:animationCurve
                     animations:^{
                         self.view.frame = CGRectMake(0, -keyboardFrame.size.height + 70, 320, 416);
                     }
                     completion:^(BOOL finished){
                         NSLog(@"Slide up Done..!");
                     }];
}
于 2013-02-04T06:58:47.987 回答
0

Firstofall 添加<UITextFieldDelegate>在您的.h文件中而不是在.m文件中

- (void)viewDidLoad
{
 yourtextfield.delegate = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == yourtextfield)
    {
       [self  showAnimationPests];
    }
}
-(void) showAnimationPests
{
 [UIView animateWithDuration:0.5 
             animations:^{
                 self.view.frame = CGRectMake(0,-300,1024,768);
             }];

 }
于 2013-02-04T06:55:49.073 回答