首页 > 其他 > 详细

UIGestureRecognizer 手势想知道你点到了哪个控件吗,点这里

时间:2021-01-19 20:01:50      阅读:6      评论:0      收藏:0      [点我收藏+]

 

 

 

 

给一连串的控件增加了UIGestureRecognizer 手势的时候 ,因为控件是动态增加的,点击时不知道如何确定自己点在哪个控件上。想取控件的属性值,取不到那就看这里。
代码如下
UIGestureRecognizer* Tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTouch:)]; // 创建手势
[myImage addGestureRecognizer:Tap];// 在图片上增加手势
myImage.tag = 100;// 给图片增加一个唯一标识
- (void)imageTouch:(UITapGestureRecognizer * )sender
{
NSLog(@"%@",sender.self.view);// 这个view 就是点击的那个控件
}

 

 

例子:

    //增加一行意见模版
    [[OAApplication shareApplication] getOpinionTemplate:^(id dictionary) {
        NSArray *temps = [dictionary objectForKey:@"rows"];
        sc1.contentSize = CGSizeMake(110*temps.count+10, 105);
        for (int i = 0; i < temps.count; i++) {
            NSDictionary  * dic  = [temps objectAtIndex:i];
            NSString *template = [dic objectForKey:@"context"];
            UITextView *textV = [[UITextView alloc]initWithFrame:CGRectMake(110*i+10, 0, 95, sc1.frame.size.height)];
            textV.backgroundColor  =  [UIColor sepLineColor];
            textV.text =template;
            textV.editable = NO;
            textV.selectable = NO;
            [sc1 addSubview:textV];
            
            UITapGestureRecognizer *textVtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectTemp:)];
            [textV addGestureRecognizer:textVtap];

        }
        
        UIButton *addtemp = [[UIButton alloc]initWithFrame:CGRectMake(110*temps.count+10, 0, 100, sc1.frame.size.height)];
        [addtemp setImage:[UIImage imageNamed:@"xinjian"] forState:UIControlStateNormal];
        addtemp.backgroundColor = [UIColor sepLineColor];
        [sc1 addSubview:addtemp];
    }];



//点击意见模版,选择意见

 

- (void)selectTemp:(UITapGestureRecognizer *)sender{

 

    UITextView *textV = (UITextView *)sender.self.view;

 

    DCFLog(@"********%@ ",textV.text);

 

    _myOpinion = textV.text;

 

    _styleBopinion.text = textV.text;

 

}

 

 

 

 

UIGestureRecognizer 手势想知道你点到了哪个控件吗,点这里

原文:https://www.cnblogs.com/OIMM/p/14297761.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号