我有一个UIScrollView
,里面有一些从xib文件加载的视图。
UIScrollView
只加载三个Views
。电流,左边的和右边的。
例如,我在当前View
的左侧和右侧各有一个视图。如果我向右滚动,UIScrollView
将向左删除视图,向右滚动到新的当前View
,并将新视图加载到新的现有View
的右侧。
此外,我在UIScrollView
外面有一个按钮。当我点击它时,它会更改UIScrollView
上显示的当前视图的背景色。
它工作得很好,但有时,我不知道为什么,当我点击按钮更改视图的背景色时,视图被很好地更改了,但视图没有刷新,所以用户看不到背景色的变化。
UIScrollView:
container = [[UIScrollView alloc] initWithFrame:frame];
[container setBackgroundColor:[UIColor clearColor]];
[container setCanCancelContentTouches:NO];
[container setClipsToBounds:NO];
[container setShowsHorizontalScrollIndicator:NO];
[container setPagingEnabled:YES];
当我点击按钮更改当前视图的背景色时调用的方法
- (void)menuColor:(MenuPickerViewController *)controller didPickOption:(UIView *)button
{
// Get the object containing the data of the product linked with the view.
MyProduct *product = (MyProduct *)[MyProduct getProduct:[_slider getCurrentContentDisplayed]];
// Get the the superview of the button sender to have an access for the attributes of this button (color selected, ...)
ColorButtonMenu *colorView = (ColorButtonMenu *)[button superview];
// Get the current UIView displayed in the UIScrollView
MyView *myView = (MyView *)[self sliderGetViewWithID:[_slider getCurrentContentDisplayed] FromSlider:_slider];
// I check with debugger, the color is well setted
product.color = colorView.color;
// "border" is a view in my xib that I want change its background color.
IFPrint(@"myView.border backgorund color before: %@n", myView.border.backgroundColor.description);
[myView.border setBackgroundColor:[Utilities colorFromHexString:colorView.color]];
[myView.border setNeedsDisplay];
IFPrint(@"myView.border backgorund color after: %@n", myView.border.backgroundColor.description);
IFPrint(@"=== DEBUG ===n");
IFPrint(@"isMainThread ? : %in", [NSThread isMainThread]); // Always return YES
IFPrint(@"myView: %@n", myView); // Always return the address of a valid pointer
IFPrint(@"myView border: %@n", myView.border); // Always return the address of a valid pointer
IFPrint(@"=============nn");
}
因此,正如您在方法的末尾所看到的,我试图在从xib
加载的视图上调用方法setNeedsDisplay
,并在"border"内调用另一个视图,但都不起作用。此外,我的方法总是在主线程上调用。
有什么假设吗?
谢谢!
编辑:
显然,我已经测试了sliderGetViewWithID返回的视图是否是正确的视图。所有属性都设置好了。在我看来,这确实是一个令人耳目一新的问题。
您确定要为其设置背景色的视图确实可见吗?我想它可能在屏幕外,所以你看不到任何事情发生。
您可能希望通过查找滚动视图的边界和myView的框架之间的交集来捕获该事件。如果没有交叉点,则表示myView实际上不可见。
所以代码是:
BOOL intersects = CGRectIntersectsRect(scrollview.bounds, myView.frame);
if(intersects == NO)
{
NSLog(@"myView is offscreen");
}
问题已解决:
设置视图的背景颜色后。我从UIScrollView中删除了该视图,然后将其重新添加到UIScrollView中。这有点棘手,但效果很好!
[myView removeFromSuperView];
[myScrollView addSubview:myView];