【问题标题】:TapGestureRecognizer does not get calledTapGestureRecognizer 没有被调用
【发布时间】:2012-04-09 16:49:58
【问题描述】:

嗨,在viewDidLoad的主WiewController中我设置了一个

UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];

然后我在一个 for 循环中创建 UIViews 并将它们添加到滚动视图中,然后将其添加到主视图中。

       UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
       newsContainer.tag = countnews;
       newsContainer.userInteractionEnabled = YES;
       [newsContainer addGestureRecognizer:recognizer];            
       [tempScroll addSubview:newsContainer];

然后我有一个功能

- (void)processTap:(UITapGestureRecognizer *)recognizer {
    UIView *view = recognizer.view;
    NSLog(@"HELLO, %d", view.tag);
}

永远不会被调用,有什么建议吗?您的帮助将不胜感激。提前致谢。

这是整个.m

#import "iPadMainViewController.h"
#import "GradeintView.h"
#import <QuartzCore/CALayer.h>
#import "Category.h"
#import "News.h"
#import "LazyImageView.h"
#import "TouchView.h"

@interface iPadMainViewController ()

@end

@implementation iPadMainViewController

@synthesize detailsView = _detailsView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap:)];
    [recognizer setDelegate:self];

    GradeintView *MainTitle = [[GradeintView alloc] initWithFrame:CGRectMake(0, 0, 1024, 50)];
    GradeintView *MainSubTitle = [[GradeintView alloc] initWithFrame:CGRectMake(0, 50, 1024, 30)];

    NSMutableArray *categoriesCollection = [[Category alloc] allCategoriesFromFeedAtUrl:@"http://geonews.ge/xml/category.php"];

    UIScrollView *categories = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 512, 768)];
    _detailsView = [[UIWebView alloc] initWithFrame:CGRectMake(500, 0, 512, 768)];
    [_detailsView addGestureRecognizer:recognizer];
    [categories setScrollEnabled:TRUE];
    [categories setContentSize:CGSizeMake(500, categoriesCollection.count * 156)];

    MainTitle.layer.masksToBounds = NO;
    MainTitle.layer.shadowOffset = CGSizeMake(3, 3);
    MainTitle.layer.shadowRadius = 5;
    MainTitle.layer.shadowOpacity = 0.3;

    [categories setBackgroundColor:[UIColor redColor]];

    int counter = 0;

    for (Category *cat in categoriesCollection)
    {
        UIView *categoryTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 166 * counter
                                                                         , 500, 20)];

        UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 20)];

        [categoryLabel setBackgroundColor:[UIColor clearColor]];
        NSMutableArray *allCurrentNews = [[News alloc] allNewsFromCategory:cat.CategoryId];

        categoryLabel.text = cat.Name;
        categoryLabel.textColor = [UIColor whiteColor];

        [categoryTitle addSubview:categoryLabel];

        UIColor *myblack = [[UIColor alloc] initWithRed:0.14 green:0.14 blue:0.14 alpha:1];
        UIColor *ligheterBlac = [[UIColor alloc] initWithRed:0.227 green:0.22 blue:0.22 alpha:1];
        [categoryTitle setBackgroundColor:myblack];

        UIScrollView *tempScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 166 * counter, 500, 166)];

        UIColor *tempcolor = ligheterBlac; 
        tempScroll.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
        tempScroll.layer.borderWidth = 0.5f;
        int countnews = 0;

        for (News *news in allCurrentNews)
        {
            UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
            newsContainer.tag = countnews;
            [newsContainer addGestureRecognizer:recognizer];

            //newsContainer.NewsId = news.NewsId;
            LazyImageView *image = [[LazyImageView alloc] initWithURl:[NSURL URLWithString:news.Thumbnail]];
            image.frame = CGRectMake(0, 0 , 156, 96);
            UILabel *newsTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 96, 156, 30)];
            newsTitle.backgroundColor = myblack;
            newsTitle.numberOfLines = 2;
            newsTitle.font = [UIFont systemFontOfSize:11];
            newsTitle.text = news.Title;
            newsTitle.textColor = [UIColor whiteColor];
            newsTitle.textAlignment = UITextAlignmentCenter;

            newsContainer.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
            newsContainer.layer.borderWidth = 0.5f;

            [newsContainer addSubview:image];
            [newsContainer addSubview:newsTitle];

            countnews ++;
            [tempScroll setContentSize:CGSizeMake(allCurrentNews.count * 156, 96)];
            [tempScroll addSubview:newsContainer];
           //[image release];
        }

        [tempScroll setBackgroundColor: tempcolor];

        [categories addSubview:tempScroll];
        [categories addSubview:categoryTitle];
        [tempcolor release];
        [tempScroll release];
        counter ++;
    }

    self.detailsView.layer.masksToBounds = NO;
    self.detailsView.layer.shadowOffset = CGSizeMake(-10, 5);
    self.detailsView.layer.shadowRadius = 5;
    self.detailsView.layer.shadowOpacity = 0.3;


    [self.detailsView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.amazon.com"]]];
    [self.view addSubview:categories];
    [self.view addSubview:self.detailsView];
    [self.view addSubview:MainSubTitle];
    [self.view addSubview:MainTitle];

}

- (void)processTap:(UITapGestureRecognizer *)recognizer {
    UIView *view = recognizer.view;
    NSLog(@"HELLO, %d", view.tag);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)dealloc {
    [super dealloc];
}

- (void)loadDetailedContent:(NSString *)s
{
}

@end

【问题讨论】:

标签: iphone objective-c ios xcode cocoa-touch


【解决方案1】:

改变

initWithTarget:self.view

initWithTarget:self

编辑:
你也忘记了冒号:

initWithTarget:self action:@selector(processTap:)

EDIT2:
您已创建 _detailsView(分配了 UITapGestureRecognizer),但尚未将其添加到任何子视图中。它将如何运作?

【讨论】:

  • 假设所有代码都在'main WiewController'中调用
  • 我感到绝望:(添加冒号也没有帮助
  • _detailsView 暂时无所谓,我在做 newsContainer
【解决方案2】:

试试这个代码

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];
[newsContainer addGestureRecognizer::gestureRecognizer];
 gestureRecognizer.cancelsTouchesInView = NO;

【讨论】:

  • 改变 [self.view addGestureRecognizer:gestureRecognizer];到 [newsContainer addGestureRecognizer::gestureRecognizer];
【解决方案3】:

我认为问题是,您可能错过了在头文件中添加&lt;UIGestureRecognizerDelegate&gt;

【讨论】:

  • 是的,很遗憾,它没有帮助
  • 你设置了[recognizer setDelegate:self];还有
  • raywenderlich.com/6567/… 试试这个 tut。这可能有用
【解决方案4】:

将@selector(processTap) 改为@selector(processTap:)

因为现在你调用了不存在的方法。

【讨论】:

  • +1,这应该可以。此外,当您使用它时,请确保在您的 .h 文件中也声明了 - (void)processTap:(UITapGestureRecognizer *)recognizer; 方法
  • 我做了但还是什么都没有,反正模拟器中模拟的点击手势是点击还是我应该做其他事情?
【解决方案5】:

我认为问题在于包含您的视图的滚动视图具有自己的内部手势识别器,该识别器从您的点击手势识别器中“带走”触摸事件。尝试实现以下手势识别器委托方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

【讨论】:

  • 由于我对此不太熟悉,请您详细说明在何处以及如何添加它,我认为这是解决方案
  • 好吧,您的iPadMainViewController 是您的 TapGestureRecognizer 的代表。所以如果你把上面的方法复制到 iPadMainViewController 的实现中,它应该可以工作。
  • 哦,在你的 viewDidLoad 中,别忘了打电话给 [self.view addGestureRecognizer:gestureRecognizer] - 我想你还是错过了。
  • 我做了所有这些,但仍然没有结果,我一定是做错了什么
【解决方案6】:

您将UIGestureRecognizer 添加到UIWebview,不建议这样做。 UIWebview has its ownUIGestureRecognizer 很难覆盖。请参阅此SO question 了解更多信息。

【讨论】:

    【解决方案7】:

    您是否检查了交互设置? 交互应在图像的属性检查器中设置为“启用用户交互”。

    【讨论】:

      【解决方案8】:

      您可以先将您的gestureRecognizer 添加到self.view 并检查您的方法是否被调用...以及检查此线程
      Issue with a UITapGestureRecognizer

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        • 2018-11-25
        相关资源
        最近更新 更多