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

 

 

 

 

给一连串的控件增加了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;

 

}

 

 

 

 

posted @ 2021-01-19 14:20  OIMMZC  阅读(139)  评论(0编辑  收藏  举报