背景色随轮播图变化

2019-08-28  本文已影响0人  呵呵一笑很倾城ebing

#import "ViewController.h"

#import "CDRadianLayerView.h"

#import "DCMacro.h"

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) NSMutableArray *colorArray;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"white%@白色",[self isWhiteColor:[UIColor whiteColor]]?@"是":@"不是");

    NSLog(@"grayColor%@白色",[self isWhiteColor:[UIColor grayColor]]?@"是":@"不是");

    NSLog(@"darkGrayColor%@白色",[self isWhiteColor:[UIColor darkGrayColor]]?@"是":@"不是");

    NSLog(@"lightGrayColor%@白色",[self isWhiteColor:[UIColor lightGrayColor]]?@"是":@"不是");

    self.colorArray = [NSMutableArray array];

    CDRadianLayerView *view = [[CDRadianLayerView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENWIDTH*2/5+StatusBarHeight+400)];

    view.backgroundColor = [self stringTOColor:@"#FFFFFF"];

    view.radian = 25;

    [self.view addSubview:view];

    self.bgView = view;

    self.bgView.backgroundColor = [self mostColor:[UIImage imageNamed:@"file1.JPG"]];

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SafeTopHeight, SCREENWIDTH, 400)];

    scrollView.bounces = NO;

    scrollView.delegate = self;

    scrollView.backgroundColor = [UIColor clearColor];

    CGFloat left = 0;

    for (int i = 0; i<9; i++) {

        UIImageView *imgv = [[UIImageView alloc] initWithFrame:CGRectMake(left+15, 0, SCREENWIDTH-15*2, 400)];

        imgv.image = [UIImage imageNamed:[NSString stringWithFormat:@"file%d.JPG",i+1]];

        [scrollView addSubview:imgv];

        left += SCREENWIDTH;

        [self.colorArray addObject:[self mostColor:imgv.image]];

    }

    [self.colorArray addObject:[UIColor whiteColor]];

    scrollView.contentSize = CGSizeMake(left, 0);

    [self.view addSubview:scrollView];

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    NSInteger page = scrollView.contentOffset.x/SCREENWIDTH;

    CGFloat value = ((int)scrollView.contentOffset.x%(int)SCREENWIDTH)/SCREENWIDTH;

    self.bgView.backgroundColor = [self colorGradient:value startColor:self.colorArray[page] endColor:self.colorArray[page+1]];

}

-(UIColor *)mostColor:(UIImage*)image{

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1

    int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;

#else

    int bitmapInfo = kCGImageAlphaPremultipliedLast;

#endif

    //第一步 先把图片缩小 加快计算速度. 但越小结果误差可能越大

    CGSize thumbSize=CGSizeMake(50, 50);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL,

                                                thumbSize.width,

                                                thumbSize.height,

                                                8,//bits per component

                                                thumbSize.width*4,

                                                colorSpace,

                                                bitmapInfo);

    CGRect drawRect = CGRectMake(0, 0, thumbSize.width, thumbSize.height);

    CGContextDrawImage(context, drawRect, image.CGImage);

    CGColorSpaceRelease(colorSpace);

    //第二步 取每个点的像素值

    unsigned char* data = CGBitmapContextGetData (context);

    if (data == NULL) return nil;

    NSCountedSet *cls=[NSCountedSet setWithCapacity:thumbSize.width*thumbSize.height];

    for (int x=0; x<thumbSize.width; x++) {

        for (int y=0; y<thumbSize.height; y++) {

            int offset = 4*(x*y);

            int red = data[offset];

            int green = data[offset+1];

            int blue = data[offset+2];

            int alpha =  data[offset+3];

            NSArray *clr=@[@(red),@(green),@(blue),@(alpha)];

            [cls addObject:clr];

        }

    }

    CGContextRelease(context);

    //第三步 找到出现次数最多的那个颜色

    NSEnumerator *enumerator = [cls objectEnumerator];

    NSArray *curColor = nil;

    NSArray *MaxColor=nil;

    NSUInteger MaxCount=0;

    while ( (curColor = [enumerator nextObject]) != nil )

    {

        NSUInteger tmpCount = [cls countForObject:curColor];

        if ( tmpCount < MaxCount ) continue;

        MaxCount=tmpCount;

        MaxColor=curColor;

    }

    UIColor *color = [UIColor colorWithRed:([MaxColor[0] intValue]/255.0f) green:([MaxColor[1] intValue]/255.0f) blue:([MaxColor[2] intValue]/255.0f) alpha:([MaxColor[3] intValue]/255.0f)];

    return [self isWhiteColor:color]?[self stringTOColor:@"#F5F5F5"]:color;

}

- (UIColor *)stringTOColor:(NSString *)str{

    if (!str || [str isEqualToString:@""]) {

        return nil;

    }

    unsigned red,green,blue;

    NSRange range;

    range.length = 2;

    range.location = 1;

    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];

    range.location = 3;

    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];

    range.location = 5;

    [[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];

    UIColor *color= [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];

    return color;

}

- (BOOL)isWhiteColor:(UIColor *)color

{

    CGFloat red = 0.0;

    CGFloat green = 0.0;

    CGFloat blue = 0.0;

    CGFloat alpha = 0.0;

    [color getRed:&red green:&green blue:&blue alpha:&alpha];

    return (red>0.941&&green>0.941&&blue>0.941);

}

-(UIColor *)colorGradient:(CGFloat)value startColor:(UIColor *)startColor endColor:(UIColor *)endColor

{

    if (value < 0) value = 0;

    if (value > 1) value = 1;

    CGFloat sred = 0.0;

    CGFloat sgreen = 0.0;

    CGFloat sblue = 0.0;

    CGFloat salpha = 0.0;

    [startColor getRed:&sred green:&sgreen blue:&sblue alpha:&salpha];

    CGFloat ered = 0.0;

    CGFloat egreen = 0.0;

    CGFloat eblue = 0.0;

    CGFloat ealpha = 0.0;

    [endColor getRed:&ered green:&egreen blue:&eblue alpha:&ealpha];

    CGFloat cursorred = sred + (ered-sred)*value;

    CGFloat cursorgreen = sgreen + (egreen-sgreen)*value;

    CGFloat cursorblue = sblue + (eblue-sblue)*value;

    UIColor *color = [UIColor colorWithRed:cursorred green:cursorgreen blue:cursorblue alpha:1];

    return color;

}

@end

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, CDRadianDirection) {

    CDRadianDirectionBottom    = 0,

    CDRadianDirectionTop        = 1,

    CDRadianDirectionLeft      = 2,

    CDRadianDirectionRight      = 3,

};

@interface CDRadianLayerView : UIView

// 圆弧方向, 默认在下方

@property (nonatomic) CDRadianDirection direction;

// 圆弧高/宽, 可为负值。 正值凸, 负值凹

@property (nonatomic) CGFloat radian;

@end

#import "CDRadianLayerView.h"

@implementation CDRadianLayerView

- (void)setRadian:(CGFloat) radian

{

    if(radian == 0) return;

    CGFloat t_width = CGRectGetWidth(self.frame); // 宽

    CGFloat t_height = CGRectGetHeight(self.frame); // 高

    CGFloat height = fabs(radian); // 圆弧高度

    CGFloat x = 0;

    CGFloat y = 0;

    // 计算圆弧的最大高度

    CGFloat _maxRadian = 0;

    switch (self.direction) {

        case CDRadianDirectionBottom:

        case CDRadianDirectionTop:

            _maxRadian =  MIN(t_height, t_width / 2);

            break;

        case CDRadianDirectionLeft:

        case CDRadianDirectionRight:

            _maxRadian =  MIN(t_height / 2, t_width);

            break;

        default:

            break;

    }

    if(height > _maxRadian){

        NSLog(@"圆弧半径过大, 跳过设置。");

        return;

    }

    // 计算半径

    CGFloat radius = 0;

    switch (self.direction) {

        case CDRadianDirectionBottom:

        case CDRadianDirectionTop:

        {

            CGFloat c = sqrt(pow(t_width / 2, 2) + pow(height, 2));

            CGFloat sin_bc = height / c;

            radius = c / ( sin_bc * 2);

        }

            break;

        case CDRadianDirectionLeft:

        case CDRadianDirectionRight:

        {

            CGFloat c = sqrt(pow(t_height / 2, 2) + pow(height, 2));

            CGFloat sin_bc = height / c;

            radius = c / ( sin_bc * 2);

        }

            break;

        default:

            break;

    }

    // 画圆

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayer setFillColor:[[UIColor whiteColor] CGColor]];

    CGMutablePathRef path = CGPathCreateMutable();

    switch (self.direction) {

        case CDRadianDirectionBottom:

        {

            if(radian > 0){

                CGPathMoveToPoint(path,NULL, t_width,t_height - height);

                CGPathAddArc(path,NULL, t_width / 2, t_height - radius, radius, asin((radius - height ) / radius), M_PI - asin((radius - height ) / radius), NO);

            }else{

                CGPathMoveToPoint(path,NULL, t_width,t_height);

                CGPathAddArc(path,NULL, t_width / 2, t_height + radius - height, radius, 2 * M_PI - asin((radius - height ) / radius), M_PI + asin((radius - height ) / radius), YES);

            }

            CGPathAddLineToPoint(path,NULL, x, y);

            CGPathAddLineToPoint(path,NULL, t_width, y);

        }

            break;

        case CDRadianDirectionTop:

        {

            if(radian > 0){

                CGPathMoveToPoint(path,NULL, t_width, height);

                CGPathAddArc(path,NULL, t_width / 2, radius, radius, 2 * M_PI - asin((radius - height ) / radius), M_PI + asin((radius - height ) / radius), YES);

            }else{

                CGPathMoveToPoint(path,NULL, t_width, y);

                CGPathAddArc(path,NULL, t_width / 2, height - radius, radius, asin((radius - height ) / radius), M_PI - asin((radius - height ) / radius), NO);

            }

            CGPathAddLineToPoint(path,NULL, x, t_height);

            CGPathAddLineToPoint(path,NULL, t_width, t_height);

        }

            break;

        case CDRadianDirectionLeft:

        {

            if(radian > 0){

                CGPathMoveToPoint(path,NULL, height, y);

                CGPathAddArc(path,NULL, radius, t_height / 2, radius, M_PI + asin((radius - height ) / radius), M_PI - asin((radius - height ) / radius), YES);

            }else{

                CGPathMoveToPoint(path,NULL, x, y);

                CGPathAddArc(path,NULL, height - radius, t_height / 2, radius, 2 * M_PI - asin((radius - height ) / radius), asin((radius - height ) / radius), NO);

            }

            CGPathAddLineToPoint(path,NULL, t_width, t_height);

            CGPathAddLineToPoint(path,NULL, t_width, y);

        }

            break;

        case CDRadianDirectionRight:

        {

            if(radian > 0){

                CGPathMoveToPoint(path,NULL, t_width - height, y);

                CGPathAddArc(path,NULL, t_width - radius, t_height / 2, radius, 1.5 * M_PI + asin((radius - height ) / radius), M_PI / 2 + asin((radius - height ) / radius), NO);

            }else{

                CGPathMoveToPoint(path,NULL, t_width, y);

                CGPathAddArc(path,NULL, t_width  + radius - height, t_height / 2, radius, M_PI + asin((radius - height ) / radius), M_PI - asin((radius - height ) / radius), YES);

            }

            CGPathAddLineToPoint(path,NULL, x, t_height);

            CGPathAddLineToPoint(path,NULL, x, y);

        }

            break;

        default:

            break;

    }

    CGPathCloseSubpath(path);

    [shapeLayer setPath:path];

    CFRelease(path);

    self.layer.mask = shapeLayer;

}

@end

上一篇 下一篇

猜你喜欢

热点阅读