• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:27:13 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

@interface MenuButton : UIButton

- (void)changeBackground;

@end

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

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

@implementation MenuButton

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

- (id)initWithCoderNSCoder *)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)touchesEndedNSSet *)touches withEventUIEvent *)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 但执行后按钮的背景与以前相同。那么如何将背景更改为另一个渐变背景?谢谢



Best Answer-推荐答案


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

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

- (void)touchesEndedNSSet *)touches withEventUIEvent *)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];
}

您确实应该更好地跟踪渐变,这样您就不会经常删除并用相同的渐变替换它。也许只是显示和隐藏选定的状态,它总是位于未突出显示的版本之上。

关于iOS - 更改 UIButton 或自定义 UIButton 的渐变背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22908369/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap