iOS Coding项目片段记录(五)

1.设置UIButton  UIView 等显示渐变的背景颜色

- (void)setBackgroundColorWithButton:(UIButton *)button  firstColor:(UIColor *)firstColor lastColor:(UIColor *)lastColor{

    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.colors = @[(__bridge id)firstColor.CGColor,(__bridge id)lastColor.CGColor];

    gradientLayer.startPoint = CGPointMake(0, 0);

    gradientLayer.endPoint = CGPointMake(0, 1);

    gradientLayer.frame = CGRectMake(0, 0, CGRectGetWidth(button.frame), CGRectGetHeight(button.frame));

    [button.layer addSublayer:gradientLayer];
}
UIColor *firstColor = [UIColor colorWithRed:(128/256.0) green:(215/256.0) blue:(4/256.0) alpha:1];
    UIColor *lastColor = [UIColor colorWithRed:(41/256.0) green:(129/256.0) blue:(1/256.0) alpha:1];
    [self setBackgroundColorWithButton:_myButton1 firstColor:firstColor lastColor:lastColor];

2.给图片增加一个外围的白色边框

-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {
    
    //UIGraphicsBeginImageContext(image.size);
    //解决失真 模糊的问题
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    //圆的边框宽度为2,颜色为红色
    
    CGContextSetLineWidth(context,2);
    
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    
    CGRect rect = CGRectMake(inset, inset, image.size.width - inset *2.0f, image.size.height - inset *2.0f);
    
    CGContextAddEllipseInRect(context, rect);
    
    CGContextClip(context);
    
    //在圆区域内画出image原图
    
    [image drawInRect:rect];
    
    CGContextAddEllipseInRect(context, rect);
    
    CGContextStrokePath(context);
    
    //生成新的image
    
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newimg;
    
}

 

posted @ 2016-11-04 16:47  Qingyun_Qearl  阅读(221)  评论(0编辑  收藏  举报