// // BackgroundButton.m // XenonSDK // // Created by SAGESSE on 2019/5/29. // Copyright © 2019 SAGESSE. All rights reserved. // #import "BackgroundButton.h" UIImage* makeImage(UIColor* newValue) { if (newValue == nil) { return nil; } CGRect rect = CGRectMake(0, 0, 4, 4); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, newValue.CGColor); CGContextFillRect(context, rect); UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } @implementation BackgroundButton - (void)setBackgroundColor:(id)backgroundColor { [super setBackgroundColor:UIColor.clearColor]; // update background image. dispatch_async(dispatch_get_main_queue(), ^{ id n = makeImage([backgroundColor colorWithAlphaComponent:1.0]); id h = makeImage([backgroundColor colorWithAlphaComponent:0.8]); [self setBackgroundImage:n forState: UIControlStateNormal]; [self setBackgroundImage:n forState: UIControlStateNormal | UIControlStateSelected]; [self setBackgroundImage:h forState: UIControlStateHighlighted]; [self setBackgroundImage:h forState: UIControlStateHighlighted | UIControlStateSelected]; }); } @end