zoukankan      html  css  js  c++  java
  • (转)UIImageView响应点击事件

    使用EGOImageView显示头像,但是没有点击事件可以处理。

    不想用UIButton做空图片了,所以就想找UIImageView响应点击事件的方法。

     

    方法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

            

            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];

        }

        

    }

  • 相关阅读:
    Access操作必须使用一个可更新的查询
    SAP资料学习好地方
    Access关键词大全
    WPF零散笔记
    WPF:如何实现单实例的应用程序(Single Instance)
    WPF应用程序启动显示图片资源
    Drawable、Bitmap、Canvas和Paint的关系以及部分使用方法
    C#中一种可调用的异常处理方法
    easyui datagrid 点击列表头排序出现错乱的原因
    easyui datagrid 没数据时显示滚动条的解决方法
  • 原文地址:https://www.cnblogs.com/zhangdashao/p/4486547.html
Copyright © 2011-2022 走看看