2012-03-26 61 views
5

喜在viewDidLoad中我设置了一个TapGestureRecognizer不会被调用

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

主要WiewController然后IA 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 
+0

请参阅本[教程](HTTP://www.raywenderlich。 com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pan-and-more) – tipycalFlow 2012-03-26 07:12:26

+0

让我看看你的整个。m文件 – 2012-03-26 07:13:50

回答

5

变化

initWithTarget:self.view 

initWithTarget:self 

编辑:
你也忘了冒号:

initWithTarget:self action:@selector(processTap:) 

EDIT2:
您已经创建_detailsView(与分配UITapGestureRecognizer),但并没有将其添加到任何子视图。它将如何工作?

+0

假定所有的代码都在我做的'主WiewController' – beryllium 2012-03-26 06:54:55

+1

中调用。仍然没有 – Alexidze 2012-03-26 06:57:51

+0

@亚历山大,回答编辑 – beryllium 2012-03-26 07:03:56

2

试试这个代码

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

我做过了,还是没有结果 – Alexidze 2012-03-26 07:00:07

+0

变化[self.view addGestureRecognizer:gestureRecognizer]至[newsContainer addGestureRecognizer :: gestureRecognizer] – akk 2012-03-26 07:02:45

+0

这就是我如何做到这一点 – Alexidze 2012-03-26 07:12:20

0

我认为问题是,你可能已经错过了在头文件中添加<UIGestureRecognizerDelegate>

+0

是的,我做到了,遗憾的是它没有帮你 – Alexidze 2012-03-26 07:06:19

+0

你设置了[识别器setDelegate:self];还有 – 2012-03-26 07:08:48

+0

我现在做了,但还是什么都没有 – Alexidze 2012-03-26 07:12:01

2

变化@selector(processTap)到@selector(processTap :)

,因为现在你调用一个不存在的方法。

+0

+1,这应该做到这一点。另外,当你在它的时候,确保在'.h'文件中声明' - (void)processTap:(UITapGestureRecognizer *)识别器;'方法 – tipycalFlow 2012-03-26 07:14:09

+0

我做到了,但仍然没有任何东西,反正ho是在模拟器中模拟点击手势,是点击还是应该做其他事情? – Alexidze 2012-03-26 07:22:06

5

我认为问题在于,包含您的视图的scrollView具有自己的内部手势识别器,它可以从您的手势识别器中“取走”触摸事件。尝试实施以下手势识别的委托方法:

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

因为我对此并不陌生,请你详细解释在哪里以及如何添加这个,我认为这是解决方案 – Alexidze 2012-03-26 08:01:35

+0

那么,你的'iPadMainViewController'就是你的TapGestureRecognizer的代表。所以如果你将上面的方法复制到iPadMainViewController的实现中,它应该可以工作。 – karstux 2012-03-26 08:11:36

+0

哦,在你的'viewDidLoad'中,别忘了调用'[self.view addGestureRecognizer:gestureRecognizer]' - 我想你还是错过了。 – karstux 2012-03-26 08:13:30

0

你添加UIGestureRecognizerUIWebview,这是不推荐使用。 UIWebview has its own UIGestureRecognizer`s这是很难超过。有关更多信息,请参阅此SO question

4

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