检测何时按下自定义 UIButton
我为我的应用程序创建了一个自定义 UIButton(UIButton 子类)。在此按钮的实现中,按下按钮时如何更改背景颜色? (按下时它应该恢复到正常背景。我目前正在使用 Core Graphics 绘制渐变背景。)
我尝试在 drawRect:
中进行分支(检查 self.selected
和 self.highlighted ,但没有发生任何变化,因为在触摸事件上没有调用 drawRect
任何建议或代码示例表示赞赏。
I have created a custom UIButton (UIButton subclass) for my application. Within this button's implementation, how would I alter the background color when it is pressed? (It should revert to it's normal background when depressed. I am currently using Core Graphics to draw a gradient background.)
I have tried branching in drawRect:
(checking for self.selected
and self.highlighted
, but no change occurs because drawRect
is not being called on the touch event.
Any suggestions or code samples are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在触摸事件中调用
[self setNeedsRedraw]
。Asker 根据此答案和下面的评论提供了解决方案。
使用的解决方案
:
选择器是执行背景更改的自定义函数。
Try calling
[self setNeedsRedraw]
in your touch event.Asker provided a solution based on this answer and the comments below.
Solution
Used:
Where the selectors are custom functions that perform the background change.