gpt4 book ai didi

ios - UILabel 没有响应触摸事件

转载 作者:行者123 更新时间:2023-12-01 17:20:41 25 4
gpt4 key购买 nike

我正在使用以下代码来创建我的 View 。我的 UILabel在 ScrollView 内。现在我想要 touchesBegan 中的点击事件我的自定义标签的方法。但无法得到它。

        img1 = [[customLabel alloc] initWithFrame:CGRectMake(0,height ,280,10 )];//
[img1 setText: [self.answer_set objectAtIndex:i]];
[img1 setTextAlignment:NSTextAlignmentLeft];
[img1 setBackgroundColor:[UIColor colorWithRed:127.0f/255.0f green:165.0f/255.0f blue:234.0f/255.0f alpha:1.0f]];
img1.textColor = [UIColor whiteColor];
img1.lineBreakMode = NSLineBreakByWordWrapping;
img1.numberOfLines = 0;
[img1 sizeToFit];
img1.contentMode = UIViewContentModeScaleToFill;
img1.font = [UIFont fontWithName:@"Arial" size:14.0f] ;

CGFloat labelHeight=[self getStringHeightforLabel:img1];
NSLog(@"LABEL HEIGHT:: %f",labelHeight);
//set frame after getting height
if (labelHeight < 60.0) {
labelHeight = 60.0;
}

[img1 setUserInteractionEnabled:YES];
img1.tag = 5000;

img1.frame=CGRectMake(-5,height,285 + 10,labelHeight + 10);
img1.layer.cornerRadius = 5;


/** Shadow */
CALayer *shadowLayer = [[CALayer alloc] init];
shadowLayer.frame = CGRectMake(-5,height ,img1.frame.size.width,img1.frame.size.height);
shadowLayer.cornerRadius = 10;

shadowLayer.backgroundColor = [UIColor blueColor].CGColor;
shadowLayer.shadowColor = [UIColor colorWithRed:127.0f/255.0f green:165.0f/255.0f blue:234.0f/255.0f alpha:1.0f].CGColor;
shadowLayer.shadowOpacity = 0.6;
shadowLayer.shadowOffset = CGSizeMake(0,0);
shadowLayer.shadowRadius = 5;
[img1.layer setMasksToBounds:YES];

[shadowLayer addSublayer:img1.layer];

[self.ans_labels.layer addSublayer:shadowLayer];

height += img1.frame.size.height + 15;

并获得事件:
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];

NSLog(@"worked",nil);
//[self giveAnswer:touch.view.tag];
if(touch.view.tag == 5000){
// do here
}
}

我能够更早地获得事件,但不是今天。我做错了什么???

最佳答案

yourLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)];
tapGesture.delegate=self;
[yourLabel addGestureRecognizer:tapGesture];


- (void)labelTap:(UITapGestureRecognizer *)recognize {
}

关于ios - UILabel 没有响应触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20464760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com