在iPhone应用程序中自定义许多UIButton

时间:2012-10-18 02:09:07

标签: ios uibutton calculator iboutlet

在iPhone应用程序中,我在一个视图中有一堆UIButtons(大约60个按钮)。我使用以下方法来自定义按钮的外观。

- (void) setButtonAttributes:(UIButton *) buttonName withTitle:(NSString *)title withImage:(NSString *) imageSuffix
{
    NSString *unpressedName, *pressedName;
    pressedName = [NSString stringWithFormat:@"pressed%@.png", imageSuffix];
    unpressedName = [@"un" stringByAppendingString: pressedName];
    buttonName.layer.cornerRadius = 5;
    buttonName.clipsToBounds = YES;
    [buttonName setTitle:title forState:UIControlStateNormal];
    if ([imageSuffix isEqualToString:@""] || [imageSuffix isEqualToString:@"brown"] ||  [imageSuffix isEqualToString:@"green"])
    {
    buttonName.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [buttonName setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonName setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [buttonName setBackgroundImage:[UIImage imageNamed:unpressedName] forState:UIControlStateNormal];
    [buttonName setBackgroundImage:[UIImage imageNamed:pressedName] forState:UIControlStateHighlighted];
    }
    else
    {
    [buttonName setImage:[UIImage imageNamed:unpressedName] forState:UIControlStateNormal];
    [buttonName setImage:[UIImage imageNamed:pressedName] forState:UIControlStateHighlighted];
    }
    [buttonName addTarget:self action:@selector(playClick) forControlEvents:UIControlEventTouchDown];

}

因此,我在我的视图的 - (void)viewDidLoad中调用此方法大约60次。例如:

 [self setButtonAttributes:firstButton withTitle:@"First" withImage:@"brown"]; 

IB中可以看到一些按钮。但也有一些按钮在IB中不可见,所以我使用以下内容来呈现它们:

secondButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
secondButton.frame = CGRectMake(64, 394, BUTTOM_WIDTH, BUTTOM_HEIGHT);
secondButton.bounds = CGRectMake(0, 0, BUTTOM_WIDTH, BUTTOM_HEIGHT);
[self setButtonAttributes:secondButton withTitle:@"Second" withImage:@"brown"];    
[secondButton addTarget:self action:@selector(someSelector) forControlEvents:UIControlEventTouchUpInside];
[somePane addSubview:secondButton];  //somePane is a scrollview.

所有这60个按钮都在视图的头文件中定义了IBOutlet。例如:

 IBOutlet  UIButton *firstButton; 

当我将我的应用程序与iPhone的计算器应用程序进行比较时,我注意到在iPhone的计算器中按钮感觉比我的应用程序更加快捷。基本上,我的问题是,与iPhone的计算器相比,我的应用程序中按钮的响应时间要慢得多。我想知道它是否与我自定义按钮的方式有关,正如我在上面的代码中解释的那样?如果是这样的话,那么定制这些按钮的正确方法是什么?

0 个答案:

没有答案