UIImageView响应点击事件

本文介绍在iOS开发中处理UIImageView点击事件的两种方法:使用UITapGestureRecognizer和处理touchesBegan事件。此外,还介绍了如何通过事件源来识别不同的UIImageView实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法1:

UIImageView *testImageView = [[UIImageView alloc] init......................

testImageView.userInteractionEnabled = YES;  

        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];  

        [testImageViewaddGestureRecognizer:singleTap];  

        [singleTap release];  


- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {  

      

    //do some method.....   



方法2:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  

{  

      

    UITouch *touch = [[event allTouches] anyObject];  

      

    if ([touch view] != UIImageView)  

    {  

          

        //do some method.....  

          

    }  

      

}


只要在上面的方法中加入处理事件就OK了。


======================

获取点击事件来源


 1.绑定点击事件 

 [BTBook setUserInteractionEnabled:YES]; 

[BTBook setTag:1000];

    UITapGestureRecognizer *singleTap3 = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(GoThisPic:)];

    [BTBook addGestureRecognizer:singleTap3];

    [singleTap3 release];



2,获得事件源


-(void)GoThisPic:(UIGestureRecognizer *)gestureRecognizer

{

     UIImageView *view = [gestureRecognizer view];

     int tagvalue = view.tag;

}


当有多个同类型点击事件时,可以利用事件源得到对应的view。

例如:

    int photosCount = [tphotosList count];        

    for (int i=0; i<photosCount; i++) {

        

        NSString *photoUrl = [tphotosList objectAtIndex:i];

        

        //66.67 * 4

        int linenum = i / 4;

        int rownum = i % 4;

        

        EGOImageView *egoivVgirlPhoto = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"loading.png"]];

        [egoivVgirlPhoto setTag:i];

        [egoivVgirlPhoto setFrame:CGRectMake(10+66.67*rownum, 30+10+66.67*linenum, 65, 65)];

        [egoivVgirlPhoto setImageURL:[NSURL URLWithString:photoUrl]];

        

        [egoivVgirlPhoto setUserInteractionEnabled:YES];

        

        UITapGestureRecognizer *touchVgirlPhotoGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchPhotoAction:)];        

        [egoivVgirlPhoto addGestureRecognizer:touchVgirlPhotoGesture];

        [touchVgirlPhotoGesture release];

        

        [self.contentView addSubview:egoivVgirlPhoto];

        [egoivVgirlPhoto release];

        

    }


-(void)touchPhotoAction:(UIGestureRecognizer *)gestureRecognizer{

    

    UIView *egoivPhotoView = [gestureRecognizer view];

    

    int tPhotoIndex = [egoivPhotoView tag];

    NSLog(@"tPhotoIndex: %d", tPhotoIndex);

    

    if ([delegate respondsToSelector:@selector(doTouchPhoto:)]) {

        [delegate doTouchPhoto:tPhotoIndex];

    }

    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值