简体   繁体   English

带有CALayer +遮罩的UITableView滚动性能

[英]UITableView scrolling performance with CALayer + mask

This is not really a question because I just solved the problem, but I think the nature of the problem is common for many iPhone devs and the solution wasn't really plainly obvious so I wanted to share my experience. 这不是一个真正的问题,因为我只是解决了问题,但是我认为问题的本质对于许多iPhone开发人员来说都是常见的,解决方案并不是很明显,所以我想分享自己的经验。 To keep with the StackOverflow "question/answer" format, I'm going to put the details into the question and my solution into the answer. 为了保持StackOverflow的“问题/答案”格式,我将把详细信息放入问题中,并将解决方案放入答案中。

Details: 细节:

I had a tableview full of cells with two labels and a custom toggle switch control I put together from scratch. 我有一个充满两个标签的单元格的表格视图,以及一个我从头开始放在一起的自定义切换开关控件。 If I hid the toggle control, the tableview scrolled like a champ. 如果我隐藏切换控件,则表视图会像冠军一样滚动。 However, with the toggle control shown, scrolling was painfully choppy / slow on the iPhone 3G. 但是,在显示切换控件的情况下,iPhone 3G上的滚动令人不快/缓慢。 My tableview cells have no transparent components (other than the mask for the layer) and I'm reusing cells. 我的tableview单元格没有透明组件(该图层的蒙版除外),并且我正在重用单元格。

The custom toggle control extends UIButton and it's layer has two components - a UIImageView that contains the sliding "switch" part of the toggle and an elongated ellipse that is used as the mask, with a background drawn behind the layer in -drawRect: . 自定义切换控件扩展了UIButton,它的层有两个组件-一个UIImageView,它包含切换的滑动“开关”部分,另一个是用作遮罩的细长椭圆,其背景绘制在-drawRect:

How did I fix it? 我该如何解决? Answer coming shortly... 很快就会有答案...

I found out that the source of the slowness / choppiness was coming from having to re-composite the Layer with the mask sublayer on scrolling. 我发现慢度/断断续续的根源来自滚动时必须重新组合图层和遮罩子图层。 I guess it was having to re-draw the toggle switch every time the screen was re-drawn. 我想每次重新绘制屏幕时都必须重新绘制切换开关。

My solution is to hide the toggle switch layer/mask unless it's changing state. 我的解决方案是隐藏切换开关图层/遮罩,除非它处于更改状态。 When it's sitting fully in the on or off positions, I hide the toggle switch layer and replaced it with an image of the toggle switch. 当它完全处于打开或关闭位置时,我隐藏了拨动开关层,并用拨动开关的图像替换了它。 When the user touches the toggle, the dummy image hides, the actual toggleswitch component shows and does the animation to the opposite state. 当用户触摸切换开关时,虚拟图像将隐藏,实际的切换开关组件将显示动画并将其切换到相反的状态。 When the animation finishes, I hide the toggle switch component and show the dummy image, making sure to change the image to the current state. 动画结束时,我隐藏了切换开关组件并显示了虚拟图像,请确保将图像更改为当前状态。 This improved scrolling performance SIGNIFICANTLY...I dare say that it's almost as good as a native default tableview cell. 显着改善了滚动性能...我敢说它几乎与本机默认的tableview单元格一样好。

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

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