2012-03-26 65 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

您是否檢查了交互設置? 交互應在圖像的屬性檢查器中設置爲「用戶交互已啓用」。