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];
我嘗試添加相同的代碼到一個視圖控制器和按鈕等工作,所以我敢肯定,這是簡單的東西/愚蠢與代理/自拍等子視圖和視圖控制器之間,但是我有一個心理障礙。
你的先生是一個紳士和學者。完美的作品,我知道這是一個小/愚蠢的東西。 – KMG 2012-03-01 04:44:42
實際上,現在我有了相反的...它添加的視圖將不會註冊與該代碼的接觸。 – KMG 2012-03-01 05:12:07