简体   繁体   English

重新加载UIView渐变背景

[英]Reload UIView gradient background

I have set my UIView background colour to a CAGradient layer which works well. 我已经将UIView背景颜色设置为CAGradient图层,效果很好。 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;

I have tried both setNeedsDisplay and UIViewContentModeRedraw but neither are updating the UIview background while the app is running? 我已经尝试了setNeedsDisplay和UIViewContentModeRedraw,但是在应用程序运行时都没有更新UIview背景吗?

It looks like your gradient.colors array is not initialised properly. 看起来您的gradient.colors数组未正确初始化。 It's a bit of a mess on the right side :). 右侧有点混乱:)。

Sometimes a bit of verbosity is warranted, because it makes code more readable, ie 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];

There's a bit of a trickery. 有一些诡计。 The CoreAnimation framework expects to find CGColor objects inside the array. CoreAnimation框架希望在数组内找到CGColor对象。 But the property is of NSArray type and NSArrays really is an array of "id"s, which are "dumb" pointers to any object. 但是该属性属于NSArray类型,而NSArrays实际上是“ id”的数组,它们是指向任何对象的“哑”指针。 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. 因此,我们创建了CGColor实例(为了保持理智起见,在单独的行上),然后(出于现代目的,使用现代目标C文字:)创建了数组,由于将CoreFoundation对象(CGColors)转换为Foundation对象(id),因此需要被桥接。

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];
} 

You should not need to set the view to be dirty with setNeedsLayout and layoutIfNeeded. 您无需使用setNeedsLayout和layoutIfNeeded将视图设置为脏视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM