前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UITextField添加点击高亮状态

UITextField添加点击高亮状态

作者头像
czjwarrior
发布2018-05-28 11:02:40
1K0
发布2018-05-28 11:02:40
举报
文章被收录于专栏:岑志军的专栏岑志军的专栏

一、继承自UITextfield自定义一个SSTouchTextField

代码如下:

代码语言:javascript
复制
#import "SSTouchTextField.h"

@implementation SSTouchTextField

#pragma mark - Private

- (void)setBackgroundHighlighted:(BOOL)highlighted{
    [UIView animateWithDuration:0.3f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        if (highlighted) {
            [self setBackgroundColor:SS_LINE_COLOR];

        } else {
            [self setBackgroundColor:[UIColor clearColor]];
        }

    } completion:^(BOOL finished) {

    }];
}

#pragma mark - Overwrite

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self setBackgroundHighlighted:YES];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    [self setBackgroundHighlighted:NO];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    [super touchesCancelled:touches withEvent:event];
    [self setBackgroundHighlighted:NO];
}

// 增大点击区域
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{

    CGPoint convertPoint = [self convertPoint:point toView:self.superview];

    UIEdgeInsets edgeInset = UIEdgeInsetsMake(0, 100, 0, 100);
    CGRect edgeFrame = CGRectMake(self.view_minX - edgeInset.left,
                                  self.view_minY - edgeInset.top,
                                  self.view_width + edgeInset.left + edgeInset.right,
                                  self.view_height + edgeInset.top + edgeInset.bottom);

    CGRect convertFrame = [self.superview convertRect:edgeFrame toView:self.superview];
    if (CGRectContainsPoint(convertFrame, convertPoint)) {

        return self;
    }

    return [super hitTest:point withEvent:event];
}

@end
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-11-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档