5

Hi in the main WiewController in the viewDidLoad i set up a

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

and then i a for loop i create UIViews and add them to a scroll view, that is then added to the main view.

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

then i have a function

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

Which never gets called, any suggestions? your help would be greatly appreciated. Thanks in advance.

here is the entire .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
Alexidze
  • 183
  • 4
  • 14
  • Refer to this [tutorial](http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more) – tipycalFlow Mar 26 '12 at 07:12

8 Answers8

5

Change

initWithTarget:self.view

to

initWithTarget:self

EDIT:
You also have forgotten colon:

initWithTarget:self action:@selector(processTap:)

EDIT2:
You have created _detailsView (with assigned UITapGestureRecognizer) but have not added it to any subview. How it will work?

beryllium
  • 29,669
  • 15
  • 106
  • 125
5

I think the problem is that the scrollView, which contains your views, has its own internal gesture recognizer that is "taking away" touch events from your tap gesture recognizer. Try implementing the following gesture recognizer delegate method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}
karstux
  • 1,482
  • 11
  • 8
  • As I'm quite unfamilliar with this, can you please explain exactly where and how to add this, i think this is the solution – Alexidze Mar 26 '12 at 08:01
  • Well, your `iPadMainViewController` is your TapGestureRecognizer's delegate. So if you copy the above method into iPadMainViewController's implementation, it should work. – karstux Mar 26 '12 at 08:11
  • Oh, and in your `viewDidLoad`, don't forget to call `[self.view addGestureRecognizer:gestureRecognizer]` - I think you're still missing that. – karstux Mar 26 '12 at 08:13
  • I di all of those, but still no result, I must be doing something terribly wrong – Alexidze Mar 26 '12 at 09:30
4

Have you checked the Interaction setting? Interaction shall be set to "User Interaction Enabled" in the Attributes inspector for the image.

Vincent
  • 4,342
  • 1
  • 38
  • 37
2

try this code

UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];
[newsContainer addGestureRecognizer::gestureRecognizer];
 gestureRecognizer.cancelsTouchesInView = NO;
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
2

Change @selector(processTap) to @selector(processTap:)

Because now you calling method that doesn't exist.

Shmidt
  • 16,436
  • 18
  • 88
  • 136
  • +1, This should do it. Also, while you're at it, do ensure that `- (void)processTap:(UITapGestureRecognizer *)recognizer;` method is declared in your `.h` file as well – tipycalFlow Mar 26 '12 at 07:14
  • I did it but still nothing, anyway ho is the tap gesture simulated in the simulator, is it a click or should i be doing something else? – Alexidze Mar 26 '12 at 07:22
0

you can first add your gestureRecognizer to self.view and check your method called or not... as well check this thread
Issue with a UITapGestureRecognizer

Community
  • 1
  • 1
Banker Mittal
  • 1,918
  • 14
  • 26
0

I assume the problem is, you might have missed to add <UIGestureRecognizerDelegate> in the header file.

Raj A.N.T. Gounder
  • 490
  • 1
  • 6
  • 17
0

You're adding a UIGestureRecognizer to a UIWebview, which is not recommended. UIWebview has its ownUIGestureRecognizer`s which are tricky to over-ride. See this SO question for more info.

Community
  • 1
  • 1
tipycalFlow
  • 7,594
  • 4
  • 34
  • 45