2012-03-01 3 views
0

버튼이있는 프레임/이미지가있는 UIView 하위 클래스가 있습니다. 내보기 컨트롤러에 추가하려고하지만 그것은 나타나지만 단추 중 아무도 어떤 접촉을 등록합니다.iOS 터치로 응답하지 않는 버튼이있는 하위보기입니다.

여기 내 UIView 하위 클래스입니다.

#import "ControlView.h" 

@implementation ControlView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     CGRect slideFrame = CGRectMake(0, 402, 320, 82); 
     slider = [[UIView alloc] initWithFrame:slideFrame]; 
     slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"shelf.png"]]; 
     [self addSubview:slider]; 

     UIImage *btnImage = [UIImage imageNamed:@"NavBar_01.png"]; 
     UIImage *btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"]; 

     btnImage = [UIImage imageNamed:@"NavBar_01.png"]; 
     btnImageSelected = [UIImage imageNamed:@"NavBar_01_s.png"]; 
     btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     btn1.frame = CGRectMake(12, 28, 70, 50); 
     [btn1 setBackgroundImage:btnImage forState:UIControlStateNormal]; 
     [btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
     [slider addSubview:btn1]; 
     [slider bringSubviewToFront:btn1]; 
     [btn1 addTarget:self action:@selector(home) forControlEvents:UIControlEventTouchUpInside]; 

     btnImage = [UIImage imageNamed:@"NavBar_02.png"]; 
     btnImageSelected = [UIImage imageNamed:@"NavBar_02_s.png"]; 
     btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     btn2.frame = CGRectMake(87, 28, 70, 50); 
     [btn2 setBackgroundImage:btnImage forState:UIControlStateNormal]; 
     [btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
     [slider addSubview:btn2]; 
     [slider bringSubviewToFront:btn2]; 
     // [btn2 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside]; 

     btnImage = [UIImage imageNamed:@"NavBar_03.png"]; 
     btnImageSelected = [UIImage imageNamed:@"NavBar_03_s.png"]; 
     btn3 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     btn3.frame = CGRectMake(162, 28, 70, 50); 
     [btn3 setBackgroundImage:btnImage forState:UIControlStateNormal]; 
     [btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
     [slider addSubview:btn3]; 
     [slider bringSubviewToFront:btn3]; 
     // [btn3 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside]; 

     btnImage = [UIImage imageNamed:@"NavBar_04.png"]; 
     btnImageSelected = [UIImage imageNamed:@"NavBar_04_s.png"]; 
     btn4 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     btn4.frame = CGRectMake(237, 28, 70, 50); 
     [btn4 setBackgroundImage:btnImage forState:UIControlStateNormal]; 
     [btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected]; 
     [slider addSubview:btn4]; 
     [slider bringSubviewToFront:btn4]; 
     // [btn4 addTarget:self action:@selector() forControlEvents:UIControlEventTouchUpInside]; 

     UISwipeGestureRecognizer *recognizer; 

     recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideUp)]; 
     [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; 
     [self addGestureRecognizer:recognizer]; 

     recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideDown)]; 
     [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; 
     [self addGestureRecognizer:recognizer]; 

    } 
    return self; 
} 

-(void)slideUp 
{ 
    // Slide up based on y axis 
    CGRect frame = slider.frame; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:.75]; 
    frame.origin.y = 320; 
    slider.frame = frame; 
    [UIView commitAnimations]; 
} 

-(void)slideDown 
{ 
    CGRect frame = slider.frame; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:.75]; 
    frame.origin.y = 425; 
    slider.frame = frame; 
    [UIView commitAnimations]; 
} 
/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 

그리고 이것은 내 ViewController에 추가하는 방법입니다.

ControlView *cont = [[ControlView alloc]init]; 
    [homeView.view addSubview:cont]; 

나는 내가 가진 그러나 서브 뷰와의 ViewController 사이 .. 그래서이 대표/자기 등 뭔가 간단한/바보 확신 일했다 .. 등등의 ViewController와 버튼에 동일한 코드를 추가하는 시도 정신적 블록.

답변

5

당신이하고 있습니까?

ControlView *cont = [[ControlView alloc]init]; 

당신은

ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame]; 
+1

당신의 선생님이 신사 및 학자입니다 어떻게해야합니다. 완벽하게 작동합니다. 작고 어리 석다는 것을 알았습니다. – KMG

+0

사실, 이제 역순으로 처리되었습니다. 추가 된 뷰는 해당 코드로 접촉을 등록하지 않습니다. – KMG

관련 문제