2013-12-09 3 views
1

다음 코드를 사용하여 내보기를 만듭니다. 내 UILabel이 스크롤보기 안에 있습니다. 이제는 touchesBegan 내 맞춤 라벨의 클릭 이벤트를 원합니다. 그러나 그것을 얻을 수 없습니다. UILabel이 터치 이벤트에 응답하지 않습니다.

 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 
    } 
} 

앞서 이벤트를 얻을 수있었습니다 만, 오늘은. 내가 뭘 잘못하고 있니? 나는 다음과 같은 솔루션으로 결국 마지막이

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
[super touchesBegan:touches withEvent:event]; 
} 
+0

라벨에 –

+0

위의 코드를 제거하고 'touchesBegan'메소드를 붙여 넣고 'touchesBegan'이 라벨이 아닌보기임을 알리는 곳에서 터치 이벤트에 tapgesture를 추가 한 경우 라벨에 'tapgesture'를 사용해야합니다. – preetam

+0

I tapgesture를 사용하지 않았습니다. 이 메소드를 사용하고 싶습니다. - (void) touchesBegan : (NSSet *)은 withEvent : (UIEvent *) 이벤트를 건 드리며, – astuter

답변

2

처럼

+1

괜찮 았어 좋았다. :) –

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


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

보십시오 :

1) 내 UIScrollView에 다음 코드를 추가 :

scroll.userInteractionEnabled = YES; 
scroll.exclusiveTouch = YES; 
scroll.canCancelContentTouches = YES; 
scroll.delaysContentTouches = YES; 

2) 내가 원 터치 이벤트이므로 UILabel 유형에 UIButton을 추가했습니다. 사용자 정의, 배경색이 선명하고 레이블 크기와 프레임 크기가 동일해야합니다.

3) 마지막으로 가장 중요한 것은 UIScrollViewUIButton을 추가해야합니다.

이 모든 것들이 제가 원하는 정확한 기능을 제공합니다.

+0

나는 이러한 레이블을 프로그래밍 방식으로 만들 예정입니다. 나는 사용자 인터페이스도 활성화했습니다. – astuter

+0

예 i-droid 당신도 괜찮습니까? uerineteraction도 괜찮 았으면 터치를 시작해야합니다. 나는 ans를 업데이트했다. 다음을 따르십시오 –

+0

ok이 방법을 사용해도 좋았습니까 - (void) touchesEnded : (NSSet *) withuEvent : (UIEvent *) event ?? –

관련 문제