Iphone iOS-子类UIButton-在何处为突出显示的状态添加代码?

Iphone iOS-子类UIButton-在何处为突出显示的状态添加代码?,iphone,ios,uibutton,Iphone,Ios,Uibutton,我希望iOS应用程序中的按钮具有红色渐变。起初我是用图像来做这件事的,但后来意识到我可以用QuartzCore框架来做。我有以下实施文件: #import "RedButton.h" @implementation RedButton @synthesize gradientLayer = _gradientLAyer; - (void)awakeFromNib; { // Initialize the gradient layer self.gradientLayer =

我希望iOS应用程序中的按钮具有红色渐变。起初我是用图像来做这件事的,但后来意识到我可以用QuartzCore框架来做。我有以下实施文件:

#import "RedButton.h"

@implementation RedButton

@synthesize gradientLayer = _gradientLAyer;

- (void)awakeFromNib;
{
    // Initialize the gradient layer
    self.gradientLayer = [[CAGradientLayer alloc] init];
    // Set its bounds to be the same of its parent
    [self.gradientLayer setBounds:[self bounds]];
    // Center the layer inside the parent layer
    [self.gradientLayer setPosition:
     CGPointMake([self bounds].size.width/2,
                 [self bounds].size.height/2)];

    // Insert the layer at position zero to make sure the 
    // text of the button is not obscured
    [[self layer] insertSublayer:self.gradientLayer atIndex:0];

    // Set the layer's corner radius
    [[self layer] setCornerRadius:5.0f];
    // Turn on masking
    [[self layer] setMasksToBounds:YES];
    // Display a border around the button 
    // with a 1.0 pixel width
    [[self layer] setBorderColor:[UIColor colorWithRed:(158.0f/255.0f) green:0.0f blue:0.0f alpha:1.0f].CGColor];
    [[self layer] setBorderWidth:1.0f];

    [self.gradientLayer setColors:[NSArray arrayWithObjects:
                               (id)[[UIColor colorWithRed:(214.0f/255.0f) green:0.0f blue:0.0f alpha:1.0f] CGColor], 
                               (id)[[UIColor colorWithRed:(141.0f/255.0f) green:0.0f blue:0.0f alpha:1.0f] CGColor], nil]];

    [[self layer] setNeedsDisplay];

}

- (void)drawRect:(CGRect)rect;
{
    [super drawRect:rect];
}

- (void)dealloc {
    // Release our gradient layer
    self.gradientLayer = nil;
    [super dealloc];
}
@end
第一个问题-我在这里使用awakeFromNib正确吗?还是应该使用initWithFrame

第二个问题-最初我使用图像和interface builder设置按钮的默认和突出显示状态。现在我没有使用图像,如何设置按钮高亮显示时的外观?我只想反转梯度

第三个问题——我在一些地方看到它写的,你不应该把UIButton子类化。如果没有,我将如何改变我的所有按钮,使其具有这种渐变,而不复制大量代码


提前感谢。

编辑1:误读了有关图像的部分。 您应该能够使用渐变将按钮状态设置为图像

// your code for setting up the gradient layer comes first
UIGraphicsBeginImageContext(CGSizeMake(1, [self bounds].size.height));
[gradientLayer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *bgImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setBackGroundImage:bgImage forState:UIControlStateWhatever] // replace with correct states
============================

我建议将初始化代码放在函数中,而不是
awakeFromNib
(对于按钮实际上没有在nib中使用,但可能是在代码中创建的情况)。您应该创建一个自定义初始化函数,并在
initWithCoder
initWithFrame
中调用它,这两个函数都显示了一个很好的模式

您可以通过调用

[self setBackGroundImage: forState];
在这种情况下,您的状态将为
uicontrol状态突出显示

除此之外,在这种情况下反对子类化的一个论点是,您实际上并没有定义任何自定义行为,您只是试图重用一些样式代码。这里不需要子类,您可以做一些简单的事情,比如在某个地方创建一个格式化函数(可能在viewcontroller中,或者在另一个类中的某个函数),将UIButton作为场景,并在其上执行所有初始化代码。这样,您就不会将按钮锁定在子类中(如果您最终实际使用另一个UIButton子类,这将非常有用。)例如,我喜欢使用一个定义自定义触摸行为的子类,该子类允许按钮为非矩形形状(并限制其触摸区域)

我看到的另一个论点是UIButton包含一些工厂函数,这些函数可能返回与子类不同类型的按钮,但如果不使用这些函数,则可能永远不会遇到此问题