IOS开发--给UIImageView添加touch事件

 

IOS开发--给UIImageView添加touch事件

分类: IPhone开发中级系列   942人阅读  评论(1)  收藏  举报

Add Tap gesture UITapGestureRecognizer to myImageView view (type of UIImageView).

UITapGestureRecognizer *myTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTapEvent:)];
 myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:myTapGesture];
[myTapGesture release];    

Implement gestureTapEvent: method to receive the touch event.

-(void)gestureTapEvent:(UITapGestureRecognizer *)gesture {
UIImageView* myImageView = (UIImageView*)gesture.view ;
}


   
   

Then when you implement your imageViewClicked method, you can get the tapped ImageView using the view property of the GestureRecognizer. Starting from that, you can for example:

  • use the tag of your imageView (if you affected it in your tableView:cellForRowAtIndexPath:method) to retrieve the tag and do whatever you want with it (depending on what you affected it to, for example you may have set imageView.tag = indexPath.row intableView:cellForRowAtIndexPath: and get that indexPath row back then)
  • Go thru the superviews of the imageView up to the UITableViewCell, then ask for its indexPath to get it back and do whatever you want with it.

Example:

-(void)imageViewClicked:(UITapGestureRecognizer*)gestRecognizer
{
    UIImageView* iv = (UIImageView*)gestRecognizer.view;
    NSInteger tag = iv.tag; // then do what you want with this

    // or get the cell by going up thru the superviews until you find it
    UIView* cellView = iv;
    while(cellView && ![cellView isKindOfClass:[UITableViewCell class]])
        cellView = cellView.superview; // go up until you find a cell

    // Then get its indexPath
    UITableViewCell* cell = (UITableViewCell*)cellView;
    NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
}



   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值