zoukankan      html  css  js  c++  java
  • UIButton按钮的高亮状态颜色

    首先是adjustsImageWhenHighlighted属性的正确使用:

    UIButton的adjustsImageWhenHighlighted属性是当UIButton设置了背景图片时,并且没有设置高亮状态下的背景图片,点击按钮是否有高亮状态。

    默认下是YES,也就是说当我们点击按钮的时候会有高亮状态,当我们设置button.adjustsImageWhenHighlighted = NO;时,再点击图片就看不到高亮状态了。

     

    想取消按钮的高亮状态,可以继承UIButton自定义按钮控件,然后在实现文件中重写下面的方法:

    // 重写系统setHighlighted方法,取消按钮点击高亮显示
    - (void)setHighlighted:(BOOL)highlighted {}

    也可以使用KVO,当按钮在高亮状态时可以进行处理,比如:

    - (void)addObserver:(UIButton *)button {
        
        [button addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew context:nil];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
        
        UIButton *button = (UIButton *)object;
        if ([keyPath isEqualToString:@"highlighted"]) {
            if (button.highlighted) {
                [button setBackgroundColor:UIColorFromHEX(0xF8F8F8)];
                return;
            }
            [button setBackgroundColor:UIColorFromHEX(0xFFFFFF)];
        }
    }

    设置按钮高亮状态下的颜色:

    [control setBackgroundImage:[UIImage ctRoundRectImageWithFillColor:UIColorFromHEX(0xFAFAFA) cornerRadius:0] forState:UIControlStateHighlighted];

    也可以使用上面这个方法,模拟出无高亮状态:

    [control setBackgroundImage:[UIImage ctRoundRectImageWithFillColor:[UIColor clearColor] cornerRadius:0] forState:UIControlStateHighlighted];

    上面用到的颜色转Image的方法为:

    + (UIImage *)ctRoundRectImageWithFillColor:(UIColor *)fillColor cornerRadius:(CGFloat)cornerRadius
    {
        return [self ctRoundRectImageWithFillColor:fillColor borderColor:nil borderWidth:0.0f cornerRadius:cornerRadius];
    }
    
    + (UIImage *)ctRoundRectImageWithFillColor:(UIColor *)fillColor borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth cornerRadius:(CGFloat)cornerRadius
    {
        CGFloat halfBorderWidth = borderWidth * 0.5f;
        CGFloat w = cornerRadius + halfBorderWidth;
        
        CGFloat dw = w * 2 +2;
        
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(dw, dw), NO, [UIScreen mainScreen].scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(halfBorderWidth, halfBorderWidth, dw - borderWidth, dw - borderWidth) cornerRadius:cornerRadius];
        [fillColor setFill];
        [path fill];
        
        if (borderWidth > 0.0f && borderColor) {
            [borderColor setStroke];
            path.lineWidth = borderWidth;
            [path stroke];
        }
        
        CGContextAddPath(context, path.CGPath);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return [image resizableImageWithCapInsets:UIEdgeInsetsMake(w+1, w+1, w+1, w+1)];
    }
  • 相关阅读:
    linux 上安装sqlplus
    如何使用 SVN 找到一段时间内提交的代码文件
    nginx 快速检查配置文件的方法
    nginx 报错 [emerg] 1164#1664: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
    nginx 负载均衡
    C#可扩展编程之MEF学习
    5天玩转C#并行和多线程编程
    C#综合揭秘——深入分析委托与事件
    解析C#中[],List,Array,ArrayList的区别及应用
    在easyui datagrid中formatter数据后使用linkbutton
  • 原文地址:https://www.cnblogs.com/cchHers/p/12356004.html
Copyright © 2011-2022 走看看