zoukankan      html  css  js  c++  java
  • 【iOS开发笔记20/50】Tap手势

    使UIView能够支持点击的手势,需要用下面的代码:

    UITapGestureRecognizer *t = [[UITapGestureRecognizeralloc] initWithTarget:self action:@selector(singleTap:)];

    t.delegate = self;

    UIImageView *subView = (UIImageView*)[self.view viewWithTag:1234];

    [subView addGestureRecognizer:t];

    [t release];

    这里把手势加到了subView里,而没有加到整个rootView中,也就只保证了在这里面的点击手势有效,而不对subView外面的区域产生影响。

     

    另外在xib设计时,注意该控件的User Interaction Enabled 这一项要选上。

     

    然后处理singleTap方法:

     

    -(void) singleTap:(UITapGestureRecognizer*) tap {

        CGPoint p = [tap locationInView:tap.view];

        NSLog(@"single tap: %f %f", p.x, p.y );

    }

    用上面方法很容易找到单击点的坐标,再完成相应的工作即可。


    一开始不知道把手势加到指定的View中,结果在单击iAd广告时,广告的内容就是不显示,原来是singleTap把手势截获了,而广告条上得不到相应的事件了。

    从网站上查到了一种解决方案:

    先在.h文件中给类加上委托UIGestureRecognizerDelegate

    @interface myViewController : UIViewController <ADBannerViewDelegate,UIGestureRecognizerDelegate>

    然后实现下面的方法:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

        // test if our control subview is on-screen

        if ([touch.view isKindOfClass:[UIControl class]]) {

            // we touched our control surface

            returnNO; // ignore the touch

        }

        returnYES; // handle the touch

    }

    用这种办法可以使一些控件不接收到tap事件,此方法没起作用,具体原因暂时不明,以后用到时再研究。

  • 相关阅读:
    对offsetHeight,clientHeight,scrollHeight的理解
    对word-wrap和word-break的理解
    数据结构之线性表(严蔚敏《数据结构》要求)
    1-数据结构之线性表
    结构体变量声明及初始化的的那些坑
    指针与函数
    数组的深入理解
    0-绪论
    别让无知成恶趣!
    电路分析-3
  • 原文地址:https://www.cnblogs.com/speeding/p/2526488.html
Copyright © 2011-2022 走看看