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 allocinitWithTarget:selfaction:@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 allocinitWithFrame:CGRectMake(0, fOrgY, fwidth, fheight)];

    [_imageView setBackgroundColor:[UIColor clearColor]];

    [self.view addSubview:_imageView];

    

    

    _txtView = [[UITextView allocinitWithFrame: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;

    

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值