UITextField添加点击高亮状态

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

代码如下:

#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

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦