zoukankan      html  css  js  c++  java
  • IOS开发给UIImageView添加touch事件

    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 itUIView* cellView = iv;while(cellView &&![cellView isKindOfClass:[UITableViewCellclass]])
            cellView = cellView.superview;// go up until you find a cell// Then get its indexPathUITableViewCell* cell =(UITableViewCell*)cellView;NSIndexPath* indexPath =[self.tableView indexPathForCell:cell];}

  • 相关阅读:
    hdu1150&&POJ1325 Machine Schedule---最小点覆盖
    hdu-1068&&POJ1466 Girls and Boys---最大独立集
    hdu-2680 Choose the best route---dijkstra+反向存图或者建立超级源点
    hdu-1317 XYZZY---Floyd判连通+bellman最短路
    hdu-1874 畅通工程续---模板题
    hdu-2112 HDU Today---dijkstra+标号
    hdu-2066 一个人的旅行---模板题
    hdu-3790 最短路径问题---dijkstra两重权值
    hdu-2544 最短路---模板题
    BZOJ3529: [Sdoi2014]数表
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3106765.html
Copyright © 2011-2022 走看看