2014-03-06 10 views
0

간단한 커스텀 뷰에 액션을 추가하는 간단한 작업을 해왔습니다. 나는 인터넷을 통해 보면서이 "쉬운"해결책을 발견 :커스텀 뷰에 액션 추가하기

  • UITapGestureRecognizer
  • UIButton

나는 프로그램이 작업을 수행 할 수 난 그냥 수돗물을 처리해야합니다.

내 코드는 여기까지입니다. 두 솔루션을 따로 따로 사용해 보았는데 작동하지 않습니다!

하는 .m

#import "AreaView.h" 
@implementation AreaView 
#define GREY 27.0/255.0 
#define PINK_R 252.0/255.0 
#define PINK_G 47.0/255.0 
#define PINK_B 99.0/255.0 

- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName areaName:(NSString *)areaName minimumSpending:(int)minimumSpending andCapacity:(int)capacity 
{ 
    self = [self initWithFrame:frame]; 
    if (self) { 
     self.backgroundColor = [UIColor colorWithRed:GREY green:GREY blue:GREY alpha:1]; 
     self.userInteractionEnabled=YES; 
     //Init variables 
     _areaName=areaName; 
     _capacity=capacity; 
     _minimumSpending=minimumSpending; 

     //Image view 
     _logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 4, 66, 50)]; 
     //_logoImageView.image = [UIImage imageNamed:imageName]; 
     _logoImageView.backgroundColor = [UIColor grayColor]; 

     //Label 
     _areaNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 54, 76, 18)]; 
     _areaNameLabel.textAlignment = NSTextAlignmentCenter; 
     _areaNameLabel.textColor = [UIColor whiteColor]; 
     _areaNameLabel.font = [UIFont systemFontOfSize:12.0]; 
     _areaNameLabel.text = areaName; 

     //button 
     _button = [[UIButton alloc]initWithFrame:self.bounds]; 
     _button.userInteractionEnabled=YES; 
     _button.backgroundColor=[UIColor yellowColor]; 
     [_button addTarget:self action:@selector(handleTap:) forControlEvents:UIControlEventTouchUpInside]; 

     //tap gesture racognizer 
     UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; 
     [tapRecognizer setNumberOfTapsRequired:1]; 
     [tapRecognizer setDelegate:self]; 
     [self addGestureRecognizer:tapRecognizer]; 

     [self addSubview:_logoImageView]; 
     [self addSubview:_areaNameLabel]; 
     [self addSubview:_button]; 
    } 
    return self; 
} 


-(void)handleTap:(UIButton *)button 
{ 
    NSLog(@"tapped!"); 
} 

-(void)tapped:(UITapGestureRecognizer *)recognizer 
{ 
    NSLog(@"tapped!"); 
} 
@end 

.H 도와

#import <UIKit/UIKit.h> 

@interface AreaView : UIView <UIGestureRecognizerDelegate> 

@property (nonatomic) UIImageView *logoImageView; 
@property (nonatomic) UILabel *areaNameLabel; 
@property (nonatomic) NSString *areaName; 
@property (nonatomic) int minimumSpending; 
@property (nonatomic) int capacity; 
- (id) initWithFrame:(CGRect)frame imageName:(NSString *)imageName areaName:(NSString *)areaName minimumSpending:(int)minimumSpending andCapacity:(int)capacity; 
@property (nonatomic, strong) UIButton *button; 
@end 

감사합니다!

문제

편집 handleTap하고 탭 모두 내가 버튼 솔루션 또는 개별적으로 테스트 할 수있는 탭 제스처를 언급하는 경우에도 해고되지 않습니다 것입니다. 버튼 구현의 경우 인터페이스에서 볼 수 있지만 아무 것도 클릭하지 않습니다.

내 UIView는 UIScrollview에서 여러 번 (여러보기의 경우) 프로그래밍 방식으로 추가됩니다.

편집 2

문제는 그 이상 복잡하다. 커스텀 뷰는 히트 테스트를 재 작성하는 주된 기능을 가진 또 다른 커스텀 뷰 안에있는 스크롤 뷰 내부에 있으며,이 뷰의 터치는 스크롤 뷰에 의해 유지됩니다. (Here is the purpose of all that).

hittest가 관련되어있는 한 작동하지 않습니다.

+0

'작동하지 않는다'는 것은 무엇을 의미합니까? 버튼 액션이 실행됩니까? – Mani

+0

어디에서 붙였습니까? –

+0

적어도 하나의 이벤트가 있어야한다고 생각합니다. 코드를 다시 확인하십시오. – Chathurka

답변

0

이 위임 메서드를 구현하면 도움이됩니다.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 
    { 
      id touchedView = gestureRecognizer.view; 
      if ([touchedView isKindOfClass:[UIButton class]]) 
      { 
       return NO; //It won't invoke gesture method, But it'll fire button method. 
      } 
      return YES; 
    } 
+0

정확히 내가 무엇을 찾고 있었는지는 아닙니다. 나는 여러 가지 해결책을 원하지 않는다. 두 가지 테스트를 거쳤으나 버튼이나 제스처가 하나만 있으면됩니다. 감사합니다 –

+0

이 방법은 시도 할 때 이벤트가 발생하지 않습니다 ... –

0

나는 당신의 UIButtonUITapGestureRecognizer 선택기가 발사되지 않은 이유를 모르겠어요. 그러나보기에 탭을 처리 할 수있는 또 다른 옵션은 단순히 touchesEnded:withEvent 메소드를 오버라이드 (override)하는 것입니다

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    //handle a tap 
} 

그런 식으로, 당신은 전혀 UIButton 또는 UITapGestureRecognizer 객체를 생성 할 필요가 없습니다.

+0

답변을 주셔서 감사합니다하지만 불행히도 그것은 두 번째 편집에서 볼 수있는 것보다 더 복잡합니다. –

관련 문제