2012-03-26 25 views
5

Hi trong WiewController chính trong viewDidLoad tôi thiết lập mộtTapGestureRecognizer không nhận được gọi

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

và sau đó ia vòng lặp for i tạo UIViews và thêm chúng vào một cái nhìn cuộn, mà sau đó được bổ sung vào chính lượt xem.

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

sau đó tôi có một hàm

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

Mà không bao giờ được gọi, bất cứ đề nghị? Giúp đỡ của bạn sẽ được đánh giá rất nhiều. Cảm ơn trước.

đây là toàn bộ .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

Tham khảo này [hướng dẫn] (http: //www.raywenderlich. com/6567/uigesturerecognizer-guide-in-ios-5-pinches-pans-and-more) – tipycalFlow

+0

cho tôi xem toàn bộ của bạn.m tập tin –

Trả lời

5

Thay đổi

initWithTarget:self.view 

để

initWithTarget:self 

EDIT:
Bạn cũng đã quên ruột:

initWithTarget:self action:@selector(processTap:) 

EDIT2:
Bạn đã tạo _detailsView (với UITapGestureRecognizer giao) nhưng chưa thêm nó vào bất kỳ subview. Làm thế nào nó sẽ làm việc?

+0

giả định rằng tất cả các mã được gọi là trong 'chính WiewController' – beryllium

+1

tôi đã làm. Vẫn không – Alexidze

+0

@Alexidze, trả lời chỉnh sửa – beryllium

2

thử mã này

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

Tôi đã làm, vẫn không có kết quả – Alexidze

+0

thay đổi [self.view addGestureRecognizer: gestureRecognizer]; tới [newsContainer addGestureRecognizer :: gestureRecognizer]; – akk

+0

đó là cách tôi làm điều đó – Alexidze

0

tôi giả sử vấn đề là, bạn có thể đã bỏ lỡ thêm <UIGestureRecognizerDelegate> trong file header.

+0

có tôi đã làm, không may nó đã không giúp – Alexidze

+0

bạn đã đặt [bộ nhận dạngĐược ủy quyền: tự], cũng như –

+0

Tôi đã làm ngay bây giờ, nhưng vẫn không có gì – Alexidze

2

Thay đổi @selector (processTap) thành @selector (processTap :)

Vì bây giờ bạn gọi phương thức không tồn tại.

+0

+1, Điều này sẽ làm điều đó. Ngoài ra, trong khi bạn đang ở đó, hãy đảm bảo rằng trình nhận dạng '- (void) processTap: (UITapGestureRecognizer *);' phương thức được khai báo trong tệp '.h' của bạn cũng như – tipycalFlow

+0

Tôi đã làm nó nhưng vẫn không có gì, dù sao ho là chạm vào cử chỉ được mô phỏng trong trình mô phỏng, có phải là một lần nhấp hay tôi nên làm gì đó khác? – Alexidze

5

Tôi nghĩ rằng vấn đề là scrollView, chứa các chế độ xem của bạn, có trình nhận dạng cử chỉ bên trong riêng của nó là các sự kiện chạm "lấy đi" từ trình nhận dạng cử chỉ chạm của bạn. Hãy thử thực hiện các phương pháp đại biểu cử chỉ nhận dạng sau:

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

Vì tôi khá không hài lòng với điều này, bạn có thể giải thích chính xác địa điểm và cách thêm cái này, tôi nghĩ đây là giải pháp – Alexidze

+0

Vâng, 'iPadMainViewController' của bạn là đại diện của TapGestureRecognizer. Vì vậy, nếu bạn sao chép các phương pháp trên vào thực hiện của iPadMainViewController, nó sẽ làm việc. – karstux

+0

Ồ, và trong 'viewDidLoad' của bạn, đừng quên gọi' [self.view addGestureRecognizer: gestureRecognizer] '- Tôi nghĩ bạn vẫn còn thiếu điều đó. – karstux

0

Bạn đang thêm một UIGestureRecognizer đến một UIWebview, đó là không nên. UIWebview has its own UIGestureRecognizer`s rất khó để vượt qua. Xem số SO question để biết thêm thông tin.

4

Bạn đã kiểm tra cài đặt Tương tác chưa? Tương tác phải được đặt thành "Tương tác người dùng được bật" trong thanh tra Thuộc tính cho hình ảnh.

0

đầu tiên bạn có thể thêm gestureRecognizer của bạn để self.view và kiểm tra phương pháp của bạn gọi hay không ... cũng kiểm tra chủ đề này
Issue with a UITapGestureRecognizer

Các vấn đề liên quan