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

本文介绍了一个iOS应用中处理点击事件并调整TextView位置的方法。通过使用UITapGestureRecognizer,实现了单击屏幕时ImageView与TextView的位置变化,并对TextView编辑状态进行了响应式调整。

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

//.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;

    

}



源码链接: https://pan.quark.cn/s/3379ee0243ef 《Java 开发坑点解析:从根因分析到最佳实践》源码目录 书籍购买地址 京东购买 当当购买 源码说明 专栏的所有代码基于Java 8 + Spring Boot 2.2.1.RELEASE + Spring Cloud Greenwich.SR4 + Spring Data Moore-SR4开发,基于Maven做依赖管理。 每一个案例都是独立的SpringBoot或Java命令行应用程序,可以单独启动,避免相互干扰,但是它们公用一个Maven POM。 下载源码后,先在根目录运行docker-compose up命令来通过Docker运行相关的MySQL、Redis、ES、RabbitMQ等系统,随后再来启动应用。 专栏大部分内容只依赖MySQL一个组件,如果docker-compose启动有困难的话可以先注释docker-compose.yml中的相关组件,比如注释ES和RabbitMQ,等后面设计篇需要用到的时候再启动,并且需要同时删除pom.xml中的相关SpringBoot Starter模块。 源码根目录下有一个readme.md的Markdown文件,这里有一个目录列了每一篇文章对应的源码位置,同时来到每一个源码包中下面还有一个readme.md文件,里面列了每一篇文章中每一个小节的源码包名。 大多数源码中的案例都会使用wrong和right这样方法命名来代表错误实现和正确实现,你可以结合书籍内容对比实现来理解。 有一些案例(比如SQL索引一文)会基于当前时间生成测试数据,所以不确保文中的测试结果本地可以重现,需要自己调整测试用例。 书籍代码索引 说明 点击链接进...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值