gpt4 book ai didi

ios - iOS 7 分隔图像中的 UISegmentedControl 在动画期间是错误的

转载 作者:IT王子 更新时间:2023-10-29 08:15:44 24 4
gpt4 key购买 nike

我有一个自定义的 UISegmentedControl。在 iOS 6 及以下版本中它工作正常。在 iOS 7 下..在我按下控件之前它看起来很好,此时,分隔图像有一瞬间看起来很奇怪。

这是我的代码:

UIImage *segmentSelected = [[UIImage imageNamed:@"segcontrol_sel.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 6, 6)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"segcontrol_unsel.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(6, 6, 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];

当我按下 UISegmentedControl 时可能导致分隔线显示错误的任何想法?谢谢?

最佳答案

我以类似于 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 的内部结构,但至少它会让您的代码更简洁一些。

关于ios - iOS 7 分隔图像中的 UISegmentedControl 在动画期间是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18894772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com