zoukankan      html  css  js  c++  java
  • UILable添加事件

    原文:http://blog.sina.com.cn/s/blog_9e8867eb0101dk6t.html

    先需要声明的是:UILabel或UIImageView的 userInteractionEnabled属性默认为no,也就是说默认不接受事件。

     
    所以方法一:
    label.userInteractionEnabled = YES;//设置userInteractionEnabled属性为yes。
    
    UITapGestureRecognizer *labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];//初始化一个单击手势
    
    [label addGestureRecognizer:labelTap];//给label添加单击手势
    
    - (void)click:(UITapGestureRecognizer *)gesture{
        
    } 
    

    方法二:添加一个他们的子类,重写view的touch方法

    #import
    
    
    @interface untitled : UIImageView {
    
    }
    
    @end
    
    #import "untitled.h"
    
    
    @implementation untitled
    
    
    - (id)initWithFrame:(CGRect)frame {
        if ((self = [super initWithFrame:frame])) {
            // Initialization code
            self.userInteractionEnabled=YES;
        }
        return self;
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        
    }
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    
    
    @end
    


    在touch事件种添加你自己想要的操作就可以了
    然后定义自己的image就用untitled就可以了 

  • 相关阅读:
    day21继承
    day22
    面向对象
    常用模块
    模块
    迭代器
    【游记】2020-CSP
    【初赛解析】2021CSP-S初赛解析(不完全)
    【题解】AcWing 1390.通电围栏
    【题解】AcWing 1387.家的范围
  • 原文地址:https://www.cnblogs.com/SimonGao/p/4552604.html
Copyright © 2011-2022 走看看