zoukankan      html  css  js  c++  java
  • 可以触发点击事件并变色的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:)

    原理就是这么简单呢:)

     
     
  • 相关阅读:
    【MVC整理】1.使用 StructureMap 作为 ASP.NET MVC 的 DI 框架
    一个随机列表项算法
    轻量级IOC框架:Ninject
    解决“找不到请求的 .Net Framework 数据提供程序。可能没有安装”的问题
    Vcastr 2.2 flv 网络播放器 参数设置
    C#取汉字首字母
    IIS Express的安装与设置讲解
    清晰的图片缩略方案
    ssh远程连接ubuntu
    【Apache】在Apache中利用ServerAlias设置虚拟主机接收多个域名和设置域名泛解析
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3938631.html
Copyright © 2011-2022 走看看