将UITapGestureRecognizer添加到UIScrollViews的UIView

时间:2012-07-06 15:24:05

标签: objective-c uiview uilabel uitapgesturerecognizer

我正在开发一个Notification Center插件,我遇到了UITapGestureRecognizer的问题。我有一个UIScrollView,UIViews充当我的两个UILabel的包装器。

我从未调用附加到UITapGestureRecognizer的选择器,我不知道为什么。

编辑:好的,现在它可以工作,但仅适用于UIScrollView中的最后一个元素。我想这是因为我在循环中使用相同的UILabel。我该怎么办?

- (UIView *)view
{
if (_view == nil)
{

    self.aArray = [self.connector refreshData];

    _view = [[UIView alloc] initWithFrame:CGRectMake(2, 0, 316, 110)];

//setting the background 
    UIImage *bg = [[UIImage imageWithContentsOfFile:@"/System/Library/WeeAppPlugins/NCAppline.bundle/WeeAppBackground.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10];
    UIImageView *bgView = [[UIImageView alloc] initWithImage:bg];
    bgView.frame = CGRectMake(0, 0, 316, 110);
    [_view addSubview:bgView];

//creating the scrollview
    UIScrollView *scrView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    UITapGestureRecognizer *linkTappedRec = [[UITapGestureRecognizer alloc] 
                                 initWithTarget: self
                                 action: @selector(articleTap:)];
    linkTappedRec.numberOfTapsRequired = 1;
    linkTappedRec.enabled = YES;
    linkTappedRec.cancelsTouchesInView = YES;

//getting the number of pages required
    NSInteger viewcount = [self.aArray count]/3; 

    for (int i = 0; i <viewcount; i++) 
    { 
        CGFloat y = i * self.view.frame.size.width;

        UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 306, 23)];
        /* setting bunch of properties */

        UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 288, 85)];
                    /* setting bunch of properties */

//this will be the container
        UIView *vview = [[UIView alloc] initWithFrame:CGRectMake(y, 0,                                                      self.view.frame.size.width, self.view.frame.size.height)];
        [vview addSubview:lbl1];
        [vview addSubview:lbl2];
        vview.tag = i*3+2;

        [vview addGestureRecognizer:linkTappedRec];

        vview.userInteractionEnabled = YES;

        [scrView addSubview:vview]; 
    }

    scrView.contentSize = CGSizeMake(self.view.frame.size.width *viewcount, self.view.frame.size.height);
    scrView.pagingEnabled = YES;


    [_view addSubview:scrView];  

}

return _view;
}

- (void)articleTap:(UITapGestureRecognizer *)sender {
NSLog("tap");
}

0 个答案:

没有答案