重新加载 UIView 渐变背景 [英] Reload UIView gradient background

查看:27
本文介绍了重新加载 UIView 渐变背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 UIView 背景颜色设置为 CAGradient 层,效果很好.我正在尝试在应用程序运行时更改该渐变.如果我关闭并重新加载应用程序,则会应用新的渐变,但我希望它在运行时工作.

I have set my UIView background colour to a CAGradient layer which works well. I am trying to change this gradient within the application when it is running. If i close and reload the app the new gradient is applied but i want this to work during run time.

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.backGroundView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], color.CGColor, nil];

[self.backGroundView.layer insertSublayer:gradient atIndex:0];

[self.backGroundView setNeedsDisplay];
self.backGroundView.contentMode = UIViewContentModeRedraw;

我已经尝试了 setNeedsDisplay 和 UIViewContentModeRedraw,但都没有在应用运行时更新 UIview 背景?

I have tried both setNeedsDisplay and UIViewContentModeRedraw but neither are updating the UIview background while the app is running?

推荐答案

您的 gradient.colors 数组似乎未正确初始化.右边有点乱:)

It looks like your gradient.colors array is not initialised properly. It's a bit of a mess on the right side :).

有时需要一点冗长,因为它使代码更具可读性,即将问题分成多行,因此您的大脑更能控制,因为您可以更好地了解正在发生的事情.因此,让我们在单独的行上初始化颜色,看看它是否有效.

Sometimes a bit of verbosity is warranted, because it makes code more readable, i.e. you split the problem into multiple lines thus your brain is more in control because you better see what is going on. Therefore let's initialise the colors on separate line a see if it works.

    CAGradientLayer *gradient = [CAGradientLayer layer];

    CGColorRef color1 = [UIColor redColor].CGColor;
    CGColorRef color2 = [UIColor greenColor].CGColor;
    gradient.colors = @[  (__bridge id)color1  ,  (__bridge id)color2  ];

    [self.view.layer insertSublayer:gradient atIndex:0];

有点诡计.CoreAnimation 框架期望在数组中找到 CGColor 对象.但是该属性是 NSArray 类型的,而 NSArrays 实际上是一个id"数组,它们是指向任何对象的哑"指针.所以我们创建了我们的 CGColor 实例(为了理智,在单独的行上),然后我们(为了上帝的缘故,使用现代目标 C 文字:) 创建数组,因为你正在将 CoreFoundation 对象(CGColors)转换为 Foundation 对象(id)它需要被桥接.

There's a bit of a trickery. The CoreAnimation framework expects to find CGColor objects inside the array. But the property is of NSArray type and NSArrays really is an array of "id"s, which are "dumb" pointers to any object. So we create our CGColor instances (on separate lines for sanity), and then we (using modern objective C literals for gods sake :) create the array and since you are casting CoreFoundation objects (CGColors) to Foundation object (id) it needs to be bridged.

现在您也可以在运行时重用代码,例如在按下按钮的动作中.由于您已经在图层层次结构中有一个渐变图层,因此您需要获得对它的引用.(第一行做)

Now you can reuse the code also in runtime, for example inside a button pressed action. Since you already have a gradient layer in layer hierarchy there already, you need to get a reference to it. (first line does it)

- (IBAction)changeColorsOnbuttonPressed:(id)sender
{
    CAGradientLayer *gradient = [self.view.layer.sublayers firstObject];

    CGColorRef color1 = [UIColor blueColor].CGColor;
    CGColorRef color2 = [UIColor yellowColor].CGColor;
    gradient.colors = @[(__bridge id)color1, (__bridge id)color2];
} 

您不需要使用 setNeedsLayout 和 layoutIfNeeded 将视图设置为脏.

You should not need to set the view to be dirty with setNeedsLayout and layoutIfNeeded.

这篇关于重新加载 UIView 渐变背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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