我有一个 UIButton 触发一个目标动作,导致一个视图在 touchupinside 上转换到另一个视图。
发生这种情况时,我还将按钮的状态更改为选中,但所选状态的(不同)背景图像在 UIView 转换完成之前实际上并没有绘制(或者有时它似乎也随着 UIView 转换进行转换。)
如何在开始视图转换之前强制按钮重绘新的背景图像?
在 UIControlEventTouchUpInside 上运行的代码:
RhSubTab *activeTab;
for(RhSubTab *tab in self.tabViews.copy) {
tab.selected = NO;
[self.view bringSubviewToFront:tab];
if(tab.tag == type) activeTab = tab;
}
[self.view bringSubviewToFront:activeTab];
[activeTab setSelected:YES];
UIView *oldView;
UIView *newView;
for(UIView *view in self.contentViews.copy) {
if([self.view.subviews containsObject:view]) {
oldView = view;
}
if(view.tag == type) {
newView = view;
}
}
if(!oldView) [self.view addSubview:newView];
else [UIView transitionFromView:oldView toView:newView duration:0.5 options:(UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationCurveEaseInOut) completion:nil];
和自定义(RhSubTab)按钮类:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self) {
[self setBackgroundImage:[[UIImage imageNamed:@"SubContentTabBg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 9, 0, 40)] forState:UIControlStateNormal];
[self setBackgroundImage:[[UIImage imageNamed:@"SubContentTabBg-selected.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 9, 0, 40)] forState:UIControlStateSelected];
}
return self;
}