0

我正在开发一个通知中心插件,但我在使用 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");
}
4

0 回答 0