ios - UILabel 不触发 touchesBegan

标签 ios iphone objective-c ipad

我目前正在努力使 touchesBegan 起作用。

我目前有这个设置

[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);

}

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

最佳答案

好吧,在解决了我的问题之后,问题是由 UIScrollView 引起的介于我的UILabel之间我和我的 UIViewController

所以我为 UIScrollView 实现了一个类别将触摸传递给它的 super 或 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在中间

关于ios - UILabel 不触发 touchesBegan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19868393/

相关文章:

ios - 如何在 swift ios xcode 中播放本地容器中的音频声音文件

ios - 使用 SpriteKit 进行慢动作

ios - tableview单元格内的 View 原点在ios中显示错误的框架

ios - 在锁屏iOS7上显示音乐播放控件的最佳方法

ios - NSRunLoop 有时会使应用程序无响应

ios - 文本字段中的默认文本

ios - 静态库中的类别方法符号在主可执行文件中不可见

ios - Swift 将 Int[] 转换为 Int 变量

iphone - 描边和填充不能一起工作 - Objective C

objective-c - 根据 segue 标识符执行 dismissModalViewControllerAnimated 或 popViewControllerAnimated?