向UIView添加渐变的现代技术 [英] Modern technique of adding gradient to UIView

查看:79
本文介绍了向UIView添加渐变的现代技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道几种向UIView添加背景渐变的方法。我想知道什么是最有效和可扩展的方式,为什么?以下是我使用的技术:

I know of several ways of adding background gradients to UIView. I was wondering what is the most efficient and scalable way of doing so, and why? Here are the techniques I've used:


  • 创建UIView的子视图并覆盖drawRect,我在当前绘制渐变上下文。

  • Create subview of UIView and overwrite the drawRect, where I draw the gradient in current context.

a。当使用上面的渐变创建它时,我想要装饰视图的边界,并将此背景渐变作为第一个子视图插入,即 - insertSubview:atIndex:

a. When using the above gradient create it with the bounds of view I want to decorate and insert this background gradient as first subview, i.e. – insertSubview:atIndex:

b。从上面得到背景渐变视图后,我在图像上下文中渲染它并将其用作背景图像,即

b. After I get the background gradient view from above, I render it in image context and use it as background image, i.e.


UIGraphicsBeginImageContext(gradView.bounds.size);
[gradView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *gradientImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIColor *background = [UIColor colorWithPatternImage:gradientImg];
self.view.backgroundColor = background;


  • 创建可伸缩的PNG并使用它来装饰视图背景。技术与你装饰UIButton的方式非常相似。

  • Create stretchable PNG and use that to decorate view background. Technique is very similar to the way you decorate UIButton.

    
    -(UIColor *)backgroundColor:(CGRect)frame
    {
        UIImage *bg = [UIImage imageNamed:@"background_23x36"];
        bg = [bg resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 11.0, 0.0, 11.0)];
        UIGraphicsBeginImageContextWithOptions(frame.size, YES, [[UIScreen mainScreen] scale]);
        [bg drawInRect:frame];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return [UIColor colorWithPatternImage:image];
    }
    


  • 创建UIView的子视图,但不要覆盖drawRect,而是使用CAGradientLayer。与 http:// nscookbook相似。 com / 2013/04 / recipe-20-using-cagradient-layer-in-a-custom-view /

    那么添加渐变作为背景的最有效和可扩展的方法是什么?

    So what is most efficient and scalable way to add gradient as a background?

    推荐答案

    正如上面提到的Fogmeister所做,请在 drawRect:方法中进行您的 UIView 子类。

    As Fogmeister suggested above, do it in the drawRect: method of your UIView subclass.

    - (void)drawRect:(CGRect)rect
    {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        NSArray *gradientColors = [NSArray arrayWithObjects:(id) [UIColor redColor].CGColor, [UIColor yellowColor].CGColor, nil];
    
        CGFloat gradientLocations[] = {0, 0.50, 1};
        CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) gradientColors, gradientLocations);
    
        CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
        CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
    
        CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
        CGGradientRelease(gradient);
        CGColorSpaceRelease(colorSpace);
    }
    

    这篇关于向UIView添加渐变的现代技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆