可以触发点击事件并变色的UILabel

可以触发点击事件并变色的UILabel

谁说UILabel不能够当做button处理点击事件呢?今天,笔者就像大家提供一个改造过的,能够触发点击事件并变色的UILabel:)

效果图:

还能当做计时器用囧:

源码如下:

TapLabel.h 与 TapLabel.m

//
//  TapLabel.h
//  TapLabel
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>
@class TapLabel;

@protocol TapLabelDelegate <NSObject>
- (void)tapLabelEvent:(TapLabel *)label;
@end

@interface TapLabel : UILabel

@property (nonatomic, assign) id<TapLabelDelegate>  delegate;         // 协议
@property (nonatomic, strong) NSString             *notificationName; // 设置通知中心名字
@property (nonatomic, strong) NSDictionary         *metaData;         // 元数据

@end
//
//  TapLabel.m
//  TapLabel
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "TapLabel.h"

@implementation TapLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled       = YES;
        UILongPressGestureRecognizer *tap = 
        [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                      action:@selector(labelEvent:)];
        tap.minimumPressDuration          = 0.01f;
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)labelEvent:(UILongPressGestureRecognizer *)gesture
{
    // 获取到坐标值
    CGPoint locationPoint = [gesture locationInView:self];
    
    // 状态1
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        self.highlighted = YES;
    }
    
    // 状态2
    if(gesture.state == UIGestureRecognizerStateChanged)
    {
        if (locationPoint.x <= self.bounds.size.width && locationPoint.x >= 0 &&
            locationPoint.y <= self.bounds.size.height && locationPoint.y >= 0)
        {
            self.highlighted = YES;
        }
        else
        {
            self.highlighted = NO;
        }
    }
    
    // 状态3
    if (gesture.state == UIGestureRecognizerStateEnded)
    {
        if (locationPoint.x <= self.bounds.size.width && locationPoint.x >= 0 &&
            locationPoint.y <= self.bounds.size.height && locationPoint.y >= 0)
        {
            
            if (_delegate) {
                [_delegate tapLabelEvent:self];
            }
            
            if (_notificationName) {
                [[NSNotificationCenter defaultCenter] postNotificationName:_notificationName
                                                                    object:nil
                                                                  userInfo:@{@"TapLabel": self}];
            }
        }
        
        self.highlighted = NO;
    }
}

@end

使用时的源码:

//
//  RootViewController.m
//  TapLabel
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "TapLabel.h"

@interface RootViewController ()<TapLabelDelegate>

@end

@implementation RootViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    TapLabel *tap            = [[TapLabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
    tap.textAlignment        = NSTextAlignmentCenter;
    tap.center               = self.view.center;
    tap.text                 = @"YouXianMing";
    tap.font                 = [UIFont fontWithName:@"HelveticaNeue-Thin" size:18];
    tap.delegate             = self;
    tap.metaData             = @{@"name": @"YouXianMing"};
    
    tap.highlightedTextColor = [UIColor redColor];
    [self.view addSubview:tap];
}

- (void)tapLabelEvent:(TapLabel *)label
{
    NSLog(@"%@", label.metaData);
}

@end

原理解析:

1. 在初始化的时候后添加了手势处理:

2. 精确计算手势的3种状态

3. UILabel自带了highlightedTextColor:)

原理就是这么简单呢:)

 
 
原文地址:https://www.cnblogs.com/YouXianMing/p/3938631.html