• 可以触发点击事件并变色的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:)

    原理就是这么简单呢:)

     
     
  • 相关阅读:
    用户态与内核态 & 文件流与文件描述符 简介【转】
    内核模块中filp->open对文件的读写【转】
    MSM8909的触摸屏驱动导致的熄屏后重新亮屏速度慢的原因!【转】
    从基本理解到深入探究 Linux kernel 通知链(notifier chain)【转】
    LTPS、IGZO、OLED都是什么?【转】
    NB-IoT有三种部署方式及特点【转】
    NB-IoT是怎么工作的,是否支持基站定位?【转】
    【Go入门教程9】并发(goroutine,channels,Buffered Channels,Range和Close,Select,超时,runtime goroutine)
    【Go入门教程8】interface(interface类型、interface值、空interface{}、嵌入interface、反射)
    【Go入门教程7】面向对象(method、指针作为receiver、method继承、method重写)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3938631.html
Copyright © 2020-2023  润新知