我知道向UIView添加背景漸變的幾種方法。我想知道什麼是最有效和可擴展的方式,爲什麼?下面是我使用的技術:向UIView添加漸變的現代技術
創建的UIView子視圖並覆蓋drawRect中,在那裏我畫在目前情況下的梯度。
a。當使用上面的漸變創建它與視圖的邊界我想裝飾和插入此背景漸變作爲第一個子視圖,即
– insertSubview:atIndex:
b。後我從上面的背景漸變視圖,我呈現在圖像上下文,並使用它作爲背景圖像,即
UIGraphicsBeginImageContext(gradView.bounds.size); [gradView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *gradientImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIColor *background = [UIColor colorWithPatternImage:gradientImg]; self.view.backgroundColor = background;
創建伸縮性PNG並用它來裝飾視圖背景。技巧與你裝飾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/
那麼什麼是最有效和可擴展的方式來添加漸變作爲背景?
我會在視圖的drawRect中做到這一點。使用CGGradient – Fogmeister