使用CALayer + mask的UITableView滚动性能 [英] UITableView scrolling performance with CALayer + mask

查看:111
本文介绍了使用CALayer + mask的UITableView滚动性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个真正的问题,因为我刚刚解决了这个问题,但我认为问题的本质对于许多iPhone开发者来说很常见,而且解决方案并不是很明显,所以我想分享一下我的经验。为了与StackOverflow问题/答案格式保持一致,我将把问题的细节和我的解决方案放到答案中。

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. To keep with the StackOverflow "question/answer" format, I'm going to put the details into the question and my solution into the answer.

详细信息:

我有一个带有两个标签的单元格的桌面视图和一个从头开始放在一起的自定义切换开关控件。如果我隐藏了切换控件,则tableview会像冠军一样滚动。然而,在显示切换控制的情况下,iPhone 3G上的滚动非常不稳定/缓慢。我的tableview单元格没有透明组件(除了图层的掩码),我正在重复使用单元格。

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. My tableview cells have no transparent components (other than the mask for the layer) and I'm reusing cells.

自定义切换控件扩展了UIButton,它的图层有两个组件 - 一个UIImageView,它包含切换的滑动开关部分和一个细长的椭圆,用作掩码,在 -drawRect:中的图层后面绘制背景。

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:.

我是如何解决的?答案即将到来......

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.

我的解决方案是隐藏切换开关层/掩码,除非它正在改变状态。当它完全位于打开或关闭位置时,我隐藏了切换开关层并用切换开关的图像替换它。当用户触摸切换时,虚拟图像隐藏,实际的切换开关组件显示并使动画处于相反状态。当动画结束时,我隐藏了切换开关组件并显示虚拟图像,确保将图像更改为当前状态。这种改进的滚动性能非常显着......我敢说它几乎和本机默认的tableview单元一样好。

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.

这篇关于使用CALayer + mask的UITableView滚动性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆