ios – 如何制作突出显示的[自定义按钮]

ios – 如何制作突出显示的[自定义按钮],第1张

概述这个问题与以下内容完全相同:            >             How to highlight a UIButton subclass?                                    2个 我创建了一个自定义按钮,因为我想要一个渐变按钮.所以我用这段代码来实现它. @implementation CustomButton- (id)initWithF 这个问题与以下内容完全相同:            >             How to highlight a UIButton subclass?                                    2个
我创建了一个自定义按钮,因为我想要一个渐变按钮.所以我用这段代码来实现它.

@H_419_12@@implementation Custombutton- (ID)initWithFrame:(CGRect)frame{ if((self = [super initWithFrame:frame])){ [self setupVIEw]; } //[self addobserver:self forKeyPath:@"highlighted" options:0 context:nil]; return self;}- (voID)awakeFromNib { [self setupVIEw];}# pragma mark - main- (voID)setupVIEw{ self.layer.cornerRadius = 10; self.layer.borderWIDth = 1.0; self.layer.bordercolor = [UIcolor colorWithRed:167.0/255.0 green:140.0/255.0 blue:98.0/255.0 Alpha:0.25].CGcolor; self.layer.shadowcolor = [UIcolor blackcolor].CGcolor; self.layer.shadowRadius = 1; [self clearHighlightVIEw]; CAGradIEntLayer *gradIEnt = [CAGradIEntLayer layer]; gradIEnt.frame = self.layer.bounds; gradIEnt.cornerRadius = 10; gradIEnt.colors = [NSArray arrayWithObjects: (ID)[UIcolor colorWithWhite:1.0f Alpha:0.4f].CGcolor,(ID)[UIcolor colorWithWhite:1.0f Alpha:0.2f].CGcolor,(ID)[UIcolor colorWithWhite:0.75f Alpha:0.2f].CGcolor,(ID)[UIcolor colorWithWhite:0.4f Alpha:0.2f].CGcolor,(ID)[UIcolor colorWithWhite:1.0f Alpha:0.4f].CGcolor,nil]; // float height = gradIEnt.frame.size.height; gradIEnt.locations = [NSArray arrayWithObjects: [NSNumber numberWithfloat:0.0f],[NSNumber numberWithfloat:0.5f],[NSNumber numberWithfloat:0.8f],[NSNumber numberWithfloat:1.0f],nil]; [gradIEnt setBackgroundcolor:[UIcolor redcolor].CGcolor]; [self.layer insertSublayer:gradIEnt atIndex:0];}

现在渐变已经完成.但是当我按下此按钮时,它上面没有突出显示的状态.

我只是想按下它的颜色已加深.
有谁知道如何实现这个?谢谢

解决方法 如果您继承UIbutton,则可以覆盖setHighlighted方法.在这里,您可以在按钮上设置不同的渐变.

@H_419_12@- (voID)setHighlighted:(BOol)highlighted { [super setHighlighted:highlighted];} 总结

以上是内存溢出为你收集整理的ios – 如何制作突出显示的[自定义按钮]全部内容,希望文章能够帮你解决ios – 如何制作突出显示的[自定义按钮]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1020804.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存