我有一个定制的UISegmentedControl.在iOS 6和以下它工作正常.在iOS 7 ..它看起来很好,直到我按下控件,在那个时候,分隔图像看起来很奇怪的一秒钟.
这是我的代码:
UIImage *segmentSelected = [[UIImage imageNamed:@"segcontrol_sel.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(6,6,6)]; UIImage *segmentUnselected = [[UIImage imageNamed:@"segcontrol_unsel.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(6,6)]; UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"segcontrol_sel_uns.png"]; UIImage *segUnselectedSelected = [UIImage imageNamed:@"segcontrol_uns_sel.png"]; [[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [[UISegmentedControl appearance] setBackgroundImage:segmentSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; [[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; [[UISegmentedControl appearance] setDividerImage:segUnselectedSelected forLeftSegmentState:UIControlStateNormal // | UIControlStateHighlighted rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; [[UISegmentedControl appearance] setDividerImage:segUnselectedSelected forLeftSegmentState:UIControlStateHighlighted rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; [[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal //| UIControlStateHighlighted) barMetrics:UIBarMetricsDefault]; [[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; UIFont *font = [UIFont systemFontOfSize:16.0f]; UIColor *textColor = [UIColor darkGrayColor]; NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: font,@"NSFontAttributeName",textColor,@"NSForegroundColorAttributeName",nil]; [[UISegmentedControl appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
解决方法
我以类似于user2128193描述的方式解决了这个问题,而不是为值更改事件添加一个目标,我将UISegmentedControl子类化并添加了这两个方法:
- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents { [super sendActionsForControlEvents:controlEvents]; if (controlEvents & UIControlEventValueChanged) { [self removeAnimationsRecursivelyForView:self]; } } - (void)removeAnimationsRecursivelyForView:(UIView *)view { [view.layer removeAllAnimations]; for (UIView *subview in [view subviews]) { [self removeAnimationsRecursivelyForView:subview]; } }
显然,这仍然不是一个完美的解决方案,因为它依赖于UISegmentedControl的内部,但至少它会使你的代码变得更加清洁.