设置Alpha作为UIScrollVIew的子视图的UIView非常慢 [英] setting Alpha for UIView that is a subview of UIScrollVIew very slow
问题描述
当我尝试时,我有
对于 UIScrollView
包含几个 UIView
setAlpha: UIView
之一,我得到1.5秒的延迟,直到 UIView
alpha
I have UIScrollView
that contains several UIView
when I try to setAlpha:
for one of the UIView
, I get 1.5 second delay till the UIView
alpha is set.
这里是
setContentOffset
下面的代码在 setAlpha:
,尽管 setAlpha:
之前是用代码编写的
Here is the code below
setContentOffset
does run before the setAlpha:
although the setAlpha:
is written before in code
-(void)setAlphaForIndex:(int)Index{
for (UIView *v in imgScroll.subviews){
if (v.tag == Index) {
[v setAlpha:0.6];
if (![self checkIfImageInScrollRange:Index]){
if (v.tag < 5)
[imgScroll setContentOffset:CGPointMake(0, 0) animated:YES];
else
[imgScroll setContentOffset:CGPointMake((Index - 5) * (CELLWIDTH) + (Index - 5 - 1) * 3, 0) animated:YES];
}
} else {
[v setAlpha:1.0];
}
}
}
推荐答案
再次遍历代码。
看起来您可以只使用循环
设置Alpha,然后再设置contentOffset
It looks like you could use the loop only for setting alpha and set the contentOffset later.
代码应为:
-(void)setAlphaForIndex:(int)Index {
for (UIView *v in imgScroll.subviews) {
if (v.tag == Index)
[v setAlpha:0.6];
else
[v setAlpha:1.0];
}
if (![self checkIfImageInScrollRange:Index]){
if (Index < 5)
[imgScroll setContentOffset:CGPointMake(0, 0) animated:YES];
else
[imgScroll setContentOffset:CGPointMake((Index - 5) * (CELLWIDTH) + (Index - 5 - 1) * 3, 0) animated:YES];
}
}
因为始终只有一个带有alpha 0.6的视图b $ b可以避免循环并提高性能。
Since there is always only one view with alpha 0.6 you can avoid the loop and improve performance.
添加一个名为transparentViewIndex的整数属性并将
初始化为-1。改进后的代码如下所示:
Add an integer property called transparentViewIndex and initialize it to -1. Improved code would then look like:
-(void)setAlphaForIndex:(int)Index{
if (self.transparentViewIndex > -1) [[imgScroll viewWithTag: transparentViewIndex] setAlpha:1.0];
[[imgScroll viewWithTag: Index] setAlpha:0.6];
self.transparentViewIndex = Index;
if (![self checkIfImageInScrollRange:Index]){
if (Index < 5)
[imgScroll setContentOffset:CGPointMake(0, 0) animated:YES];
else
[imgScroll setContentOffset:CGPointMake((Index - 5) * (CELLWIDTH) + (Index - 5 - 1) * 3, 0) animated:YES];
}
}
这篇关于设置Alpha作为UIScrollVIew的子视图的UIView非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!