iOS - 更改 UIButton 或自定义 UIButton 的渐变背景
<p><p>我用 .h 文件创建了我自己的从 UIButton 派生的类:</p>
<pre><code>@interface MenuButton : UIButton
- (void)changeBackground;
@end
</code></pre>
<p>在实现中,我尝试添加一些更改背景的方法,但到目前为止没有成功:</p>
<pre><code>#import "MenuButton.h"
#import "GradientLayers.h"
@implementation MenuButton
- (id)initWithFrame:(CGRect)frame
{
self = ;
if (self) {
;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder;
{
self = ;
if (self) {
;
}
return self;
}
- (void)commonInit;
{
// Gradient background
CAGradientLayer *gradient = ;
gradient.frame = .bounds;
// Insert background
[ insertSublayer:gradient atIndex:0];
[ setCornerRadius:10.0f];
[ setMasksToBounds:YES];
[ setBorderWidth:2];
[ setBorderColor:[ CGColor]];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// handle touch
;
if ( != UIControlStateSelected)
{
CAGradientLayer *gradient = ;
gradient.frame = .bounds;
[ insertSublayer:gradient atIndex:0];
}
else
{
CAGradientLayer *gradient = ;
gradient.frame = .bounds;
[ insertSublayer:gradient atIndex:0];
}
}
- (void)changeBackground
{
CAGradientLayer *gradient = [[ sublayers] firstObject];
gradient = ;
[ insertSublayer:gradient atIndex:0];
;
}
@end
</code></pre>
<p>部分 GradientLayer.m 文件:</p>
<pre><code>#import "GradientLayers.h"
@implementation GradientLayers
+ (CAGradientLayer*) blackBackground {
// similar to darkBlueBackground
}
+ (CAGradientLayer*) darkBlueBackground {
// Create colors
UIColor *darkOp = ;
UIColor *lightOp = ;
// Create gradient
CAGradientLayer *gradient = ;
// Set colors
gradient.colors = [NSArray arrayWithObjects:
(id)lightOp.CGColor,
(id)darkOp.CGColor,
nil];
// Shadow
gradient.shadowOffset = CGSizeMake(3.0f, 3.0f);
gradient.shadowColor = .CGColor;
gradient.shadowOpacity = 0.6;
gradient.shadowRadius = 10;
// Other options
gradient.borderWidth = 0.5f;
gradient.borderColor = .CGColor;
gradient.cornerRadius = 10;
return gradient;
}
@end
</code></pre>
<p>我的按钮带有在 commonInit 中设置的 darkBlueBackground。这不是一个问题。但是我想在用户点击按钮后更改它,到目前为止没有运气。如果我设置断点,我在 touchesEnded 或 changeBackground 但执行后按钮的背景与以前相同。那么如何将背景更改为另一个渐变背景?谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>因为 <code>.sublayers</code> 数组以从后到前的顺序 (<a href="https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/sublayers" rel="noreferrer noopener nofollow">https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/sublayers</a>) 列出,通过在索引 <code>0</code> 处插入替换层,您可以总是把它放在旧渐变后面。</p>
<p>您应该删除旧的渐变层,然后添加新的。</p>
<pre><code>- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// handle touch
;
// Remove old gradient here
[[[ sublayers] objectAtIndex:0] removeFromSuperlayer];
if ( != UIControlStateSelected)
{
CAGradientLayer *gradient = ;
gradient.frame = .bounds;
[ insertSublayer:gradient atIndex:0];
}
else
{
CAGradientLayer *gradient = ;
gradient.frame = .bounds;
[ insertSublayer:gradient atIndex:0];
}
}
- (void)changeBackground
{
// Remove old gradient here
[[[ sublayers] objectAtIndex:0] removeFromSuperlayer];
CAGradientLayer *gradient = [[ sublayers] firstObject];
gradient = ;
[ insertSublayer:gradient atIndex:0];
;
}
</code></pre>
<p>您确实应该更好地跟踪渐变,这样您就不会经常删除并用相同的渐变替换它。也许只是显示和隐藏选定的状态,它总是位于未突出显示的版本之上。</p></p>
<p style="font-size: 20px;">关于iOS - 更改 UIButton 或自定义 UIButton 的渐变背景,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22908369/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22908369/
</a>
</p>
页:
[1]