UITextView实现点击编辑框整体视图上移动,取消编辑恢复原来状态

// 原理很简单,一看代码就懂。 直接上代码

//.h

@interface MainViewController : UIViewController <UITextViewDelegate,UIGestureRecognizerDelegate> {

    BOOL                    _up;

    UIImageView             *_imageView;

    UITextView              *_txtView;

}

@end

//.m

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    //点击事件

    UITapGestureRecognizer *single = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleGestureCaptured:)];

    [single setNumberOfTapsRequired:1];

    single.delegate = self;

    [self.view addGestureRecognizer:single];

    

    float fOrgY = 40.0;

    float fwidth = self.view.bounds.size.width;

    float fheight = 200.0;

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, fOrgY, fwidth, fheight)];

    [_imageView setBackgroundColor:[UIColor clearColor]];

    [self.view addSubview:_imageView];

    

    

    _txtView = [[UITextView alloc] initWithFrame:CGRectMake(0, fOrgY + fheight + 5, fwidth, 40.0)];

    _txtView.layer.cornerRadius = 6.0;

    _txtView.delegate = self;

    _txtView.scrollEnabled = NO;

    _txtView.returnKeyType = UIReturnKeyDone; //把默认的换行按钮换成完成按钮

    _txtView.textColor = [UIColor blackColor];

    [_txtView setBackgroundColor:[UIColor whiteColor]];

    [self.view addSubview:_txtView];

}

- (void) setMySize {

    if (_up) {

        float YOffset = -40.0;

        CGRect frame = _imageView.frame;

        frame.origin.y += YOffset;

        _imageView.frame = frame;

        

        frame = _txtView.frame;

        frame.origin.y += YOffset;

        _txtView.frame = frame;

    }

    else {

        float YOffset = 40.0;

        CGRect frame = _imageView.frame;

        frame.origin.y += YOffset;

        _imageView.frame = frame;

        

        frame = _txtView.frame;

        frame.origin.y += YOffset;

        _txtView.frame = frame;

    }

}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if ([touch.view isKindOfClass:[UIButton class]]) {

        return NO;

    }

    return YES;

}


- (void)singleGestureCaptured:(UITapGestureRecognizer *)gesture {

    if (_up) {

        _up = !_up;

        [self setMySize];

    }

    [_txtView resignFirstResponder];

}


#pragma mark UITextViewDelegate

//点击了编辑框,视图整体上移动40像素

- (void)textViewDidBeginEditing:(UITextView *)textView {

    if (textView == _txtView) {

        if (!_up) {

            _up = !_up;

            [self setMySize];

        }

    }

}


// 捕获按下"完成"按钮事件

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

    if ([text isEqualToString:@"\n"]) {

        [textView resignFirstResponder];

        if (_up) {

            _up = !_up;

            [self setMySize];

        }

        return NO;

    }

    

    return YES;

    

}



你可能感兴趣的:(UITextView实现点击编辑框整体视图上移动,取消编辑恢复原来状态)