本文介绍了UI设计-处理许多UIButton的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的界面中处理大量UIButton时遇到问题.我想知道是否有人对此有第一手经验,他们是如何做到的?

I'm running into problems when dealing with a large amount of UIButtons in my interface. I was wondering if anyone had first hand experience with this and how they did it?

在处理最简单的30-80个按钮时,您会使用UIButton还是做一些复杂的事情(例如drawRect),响应触摸事件并获取触摸事件的坐标?

When dealing with 30-80 buttons most simple, a couple of complex do you just use UIButton or do something different like drawRect, respond to touch events and get the coordinates of the touch event?

最好的例子是日历,类似于Apples Calendar App的日历.您会使用drawRect绘制大多数日子,然后单击按钮将其替换为图像还是仅使用UIButtons吗?与其说是内存占用或创建按钮,不如说是它们有时发生奇怪的事情(关于它的先前的问题)以及使它们动起来的性能问题.

Best example is a calendar, similar to that of Apples Calendar App. Would you just draw most of the days using drawRect and then when you click a button replace it with an image or just use UIButtons? It's not so much the memory footprint or creating the buttons, just strange things are happening with them sometimes (previous question about it) and having performance issues animating them.

感谢您的帮助.

推荐答案

如果您的按钮上发生了奇怪的事情",则需要深入了解为什么.仅仅为了避免不了解(可能再次出现)的问题而切换架构听起来并不是一个好主意.

If "strange things are happening" with your buttons, you need to get to the bottom of why. Switching architectures just to avoid a problem that you don't understand (and might crop up again) doesn't sound like a good idea.

-drawRect:通过绘制到支持位图的上下文来工作.当在-setNeedsDisplay之后调用-displayIfNeeded时发生这种情况(或执行其他隐式设置needDisplay标志的操作,例如使用contentMode = UIContentModeRedraw调整视图的大小).然后将由位图支持的上下文合成到屏幕上.

-drawRect: works by drawing to a bitmap-backed context. This happens when -displayIfNeeded is called after -setNeedsDisplay (or doing something else that implicitly sets the needsDisplay flag, like resizing a view with contentMode = UIContentModeRedraw). The bitmap-backed context is then composited to screen.

按钮通过将不同的组件(背景图像,前景图像,文本)放在不同的层中来工作.文本在更改时被绘制并合成到屏幕上;这些图像只是直接合成到屏幕上.

Buttons work by putting the different components (background image, foreground image, text) in different layers. The text is drawn when it changes and composited to the screen; the images are just composited directly to the screen.

做事的最佳"方法通常是两者的结合.例如,您可以在-drawRect中绘制文本和背景图像:这样,在渲染时就不需要合成不同的图层(如果视图是不透明的",则可以得到额外的加速).您可能要避免通过drawRect:进行全屏动画处理(并且与CoreAnimation集成得不太好),因为绘图往往比合成更昂贵.

The "best" way to do things is usually a combination of the two. For example, you might draw text and a background image in -drawRect: so the different layers didn't need to be composited at render time (you get an additional speedup if your view is "opaque"). You probably want to avoid full-screen animations via drawRect: (and it won't integrate so well with CoreAnimation), since drawing tends to be more expensive than compositing.

但是首先,我要找出UIButton出了什么问题.几乎没有必要担心您如何才能使速度更快,直到您真正找出慢速位.编写代码以便于维护. UIButton并不是 that 昂贵的,而-drawRect:也不是 糟糕的(如果对较小的rect使用-setNeedsDisplayInRect:可能更好,但是您需要计算rect ...),但是如果您想要一个按钮,请使用UIButton.

But first, I'd find out what's going wrong with UIButton. There's little point worrying about how you could make things faster until you actually find out what the slow bits are. Write code so that it is easy to maintain. UIButton is not that expensive and -drawRect: is not that bad (presumably it's even better if you use -setNeedsDisplayInRect: for a smallish rect, but then you need to calculate the rect...), but if you want a button, use UIButton.

这篇关于UI设计-处理许多UIButton的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:26