UILabel没有触发touchesBegan

时间:2013-11-08 20:58:17

标签: ios iphone objective-c ipad

我目前正努力让触动工作。

我目前有这个设置

[UIView(UIViewController) -> UIScrollView -> UIView[holderView] -> UILabels[]]

我以编程方式添加UILabels这种方式

//UIViewController method
UILabel *etiquetaCantidad = [[UILabel alloc] initWithFrame:CGRectMake(350, idx * 35, 50, 30)];
[etiquetaCantidad setTextAlignment:NSTextAlignmentCenter];
[etiquetaCantidad setBackgroundColor:[UIColor azulBase]];
[etiquetaCantidad setTextColor:[UIColor whiteColor]];
[etiquetaCantidad.layer setCornerRadius:5];
[etiquetaCantidad setText:@"0"];
[etiquetaCantidad setUserInteractionEnabled:YES];
[etiquetaCantidad setTag:idx + 100];
[holderView addSubview:etiquetaCantidad];

但是当我尝试

// UIViewController 
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    NSLog(@"Touch realizado: %@", touches);

}

它没有被触发,我在这里错过了什么?

1 个答案:

答案 0 :(得分:1)

好吧,在我的问题挣扎之后,问题是由我UIScrollViewUILabel

之间的UIViewController引起的

所以我已经为UIScrollView实施了一个类别,将其传递给了超级或nextResponder

#import "UIScrollView+TouchesBeganPropagable.h"

@implementation UIScrollView (TouchesBeganPropagable)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    if(!self.dragging){
        [self.nextResponder touchesBegan:touches withEvent:event];
    }else{
        [super touchesBegan:touches withEvent:event];
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    if (!self.dragging){
        [self.nextResponder touchesMoved: touches withEvent:event];
    }
    else{
        [super touchesMoved: touches withEvent: event];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    if (!self.dragging){
        [self.nextResponder touchesEnded: touches withEvent:event];
    }
    else{
        [super touchesEnded: touches withEvent: event];
    }
}

@end

这样,我就可以在UIViewController上使用touchesXXXX方法与任何UIView进行互动,无论UIScrollView是否位于中间