ios - 如何获得黑色光泽的 UIButton 外观?

标签 ios uibutton

这可能吗?我在应用程序中看到过它,但我不想诉诸于使用图像文件来实现它。

最佳答案

您可以使用此代码:

-(void) addGradient:(UIButton *) _button {

    // Add Border
    CALayer *layer = _button.layer;
    layer.cornerRadius = 8.0f;
    layer.masksToBounds = YES;
    layer.borderWidth = 1.0f;
    layer.borderColor = [UIColor colorWithWhite:0.5f alpha:0.2f].CGColor;

    // Add Shine
    CAGradientLayer *shineLayer = [CAGradientLayer layer];
    shineLayer.frame = layer.bounds;
    shineLayer.colors = [NSArray arrayWithObjects:
                         (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:0.75f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,
                         (id)[UIColor colorWithWhite:1.0f alpha:0.4f].CGColor,
                         nil];
    shineLayer.locations = [NSArray arrayWithObjects:
                            [NSNumber numberWithFloat:0.0f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.5f],
                            [NSNumber numberWithFloat:0.8f],
                            [NSNumber numberWithFloat:1.0f],
                            nil];
    [layer addSublayer:shineLayer];
}

关于ios - 如何获得黑色光泽的 UIButton 外观?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7477652/

相关文章:

iPhone UIView :Is it possible to enable userinteraction only on subviews?

iOS swift,视频输出不在 UIView 内居中

ios - 如何在 Xamarin Studio 中检查预构建事件的构建配置

ios - 当设备即将解锁时会调用哪个方法?

iphone - NIB 相关的崩溃在 iPad 但不是 iPhone

iphone - 我们如何禁用 iPhone 应用程序中的按钮?

ios - 设置图像/背景图像时,UIButton 标题不会突出显示

ios - Firebase 缓存过期总是和 Debug模式一样

ios - tableFooterView 中的 UIButton 未注册触摸

ios - 向自定义 UIButton 添加约束不起作用