iOS-更改UIButton或自定义UIButton的渐变背景

里博尔·扎普勒塔尔(Libor Zapletal)

我用.h文件创建了自己的派生自UIButton的类:

@interface MenuButton : UIButton

- (void)changeBackground;

@end

在实现中,我尝试添加一些方法来更改背景,但到目前为止没有成功:

#import "MenuButton.h"
#import "GradientLayers.h"

@implementation MenuButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder;
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit;
{

    // Gradient background
    CAGradientLayer *gradient = [GradientLayers darkBlueBackground];
    gradient.frame = [self layer].bounds;

    // Insert background
    [[self layer] insertSublayer:gradient atIndex:0];

    [[self layer] setCornerRadius:10.0f];
    [[self layer] setMasksToBounds:YES];
    [[self layer] setBorderWidth:2];
    [[self layer] setBorderColor:[[UIColor blackColor] CGColor]];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // handle touch
    [super touchesEnded:touches withEvent:event];

    if ([self state] != UIControlStateSelected)
    {
        CAGradientLayer *gradient = [GradientLayers blackBackground];
        gradient.frame = [self layer].bounds;
        [[self layer] insertSublayer:gradient atIndex:0];
    }
    else
    {
        CAGradientLayer *gradient = [GradientLayers darkBlueBackground];
        gradient.frame = [self layer].bounds;
        [[self layer] insertSublayer:gradient atIndex:0];
    }
}

- (void)changeBackground
{
    CAGradientLayer *gradient = [[[self layer] sublayers] firstObject];
    gradient = [GradientLayers blackBackground];
    [[self layer] insertSublayer:gradient atIndex:0];
    [self setNeedsDisplay];
}

@end

GradientLayer.m文件的一部分:

#import "GradientLayers.h"

@implementation GradientLayers

+ (CAGradientLayer*) blackBackground {
    // similar to darkBlueBackground
}

+ (CAGradientLayer*) darkBlueBackground {
    // Create colors
    UIColor *darkOp = [UIColor colorWithRed:0.039f green:0.106f blue:0.278f alpha:1.0];
    UIColor *lightOp = [UIColor colorWithRed:0.133f green:0.267f blue:0.65f alpha:1.0];

    // Create gradient
    CAGradientLayer *gradient = [CAGradientLayer layer];

    // Set colors
    gradient.colors = [NSArray arrayWithObjects:
                       (id)lightOp.CGColor,
                       (id)darkOp.CGColor,
                       nil];

    // Shadow
    gradient.shadowOffset = CGSizeMake(3.0f, 3.0f);
    gradient.shadowColor = [UIColor blackColor].CGColor;
    gradient.shadowOpacity = 0.6;
    gradient.shadowRadius = 10;

    // Other options
    gradient.borderWidth = 0.5f;
    gradient.borderColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0].CGColor;
    gradient.cornerRadius = 10;

    return gradient;
}

@end

我在commonInit中设置了带有darkBlueBackground的按钮。这算不上问题。但是我想在用户点击按钮后更改它,到目前为止没有运气。如果设置断点,则处于touchesEnded或changeBackground,但是执行后,按钮的背景与以前相同。那么如何将背景更改为其他渐变背景呢?谢谢

尼尔

因为该[self layer].sublayers数组是按从后到前的顺序列出的(https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/sublayers),通过将替换层插入index 0,您总是将其放在旧的渐变后面。

您应该删除旧的渐变图层,然后添加新的渐变图层。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // handle touch
    [super touchesEnded:touches withEvent:event];

    // Remove old gradient here
    [[[[self layer] sublayers] objectAtIndex:0] removeFromSuperlayer];

    if ([self state] != UIControlStateSelected)
    {
        CAGradientLayer *gradient = [GradientLayers blackBackground];
        gradient.frame = [self layer].bounds;
        [[self layer] insertSublayer:gradient atIndex:0];
    }
    else
    {
        CAGradientLayer *gradient = [GradientLayers darkBlueBackground];
        gradient.frame = [self layer].bounds;
        [[self layer] insertSublayer:gradient atIndex:0];
    }
}

- (void)changeBackground
{
    // Remove old gradient here
    [[[[self layer] sublayers] objectAtIndex:0] removeFromSuperlayer];

    CAGradientLayer *gradient = [[[self layer] sublayers] firstObject];
    gradient = [GradientLayers blackBackground];
    [[self layer] insertSublayer:gradient atIndex:0];
    [self setNeedsDisplay];
}

您确实应该更好地跟踪渐变,因此您不必不断地用相同的渐变删除和替换它。也许只是显示和隐藏所选状态,而该状态始终位于未突出显示的版本上方。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS-UIButton自定义目标

来自分类Dev

自定义iOS7中的UIButton类的行为

来自分类Dev

自定义iOS7中的UIButton类的行为

来自分类Dev

iOS自定义UITableViewCell设置UIButton标题标签文本

来自分类Dev

如何为UIButton设置自定义渐变背景?

来自分类Dev

iOS应用程序中自定义UIButton旁边显示“蓝色框”

来自分类Dev

iOS自定义UIButton未出现在视图中

来自分类Dev

Xamarin.Forms - iOS - 在 UIButton 上放置多个图像的自定义效果

来自分类Dev

UIButton背景边框iOS的问题

来自分类Dev

更改自定义文本字段的背景颜色和文本颜色IOS Swift

来自分类Dev

带圆角和背景错误的iOS UIButton

来自分类Dev

自定义UIButton背景颜色不起作用

来自分类Dev

iOS 8自定义输入视图背景色

来自分类Dev

自定义设置按钮背景色iOS

来自分类Dev

Xamarin iOs:如何自定义顶栏的背景颜色

来自分类Dev

在iOS中将文本更改为uibutton

来自分类Dev

iOS:通过按下UIButton更改UIImageView

来自分类Dev

iOS更改UiButton的颜色不起作用

来自分类Dev

UIButton自定义类

来自分类Dev

创建自定义UIButton

来自分类Dev

无法更改UIButton上的自定义UIFont大小

来自分类Dev

无法更改UIButton上的自定义UIFont大小

来自分类Dev

使用UIButton的自定义UITableViewCell:无法更改UIImage

来自分类Dev

iOS 8自定义键盘:更改高度

来自分类Dev

iOS 8自定义键盘方向更改

来自分类Dev

更改自定义uibarbuttonitem(iOS7)的颜色

来自分类Dev

自定义iOS ChartBar

来自分类Dev

iOS UIButton始终透明

来自分类Dev

覆盖IOS UIButton buttonPressed