当iOS 7中显示工作表/提醒时,如何在自定义绘制的控件上将tintColor设置为灰色?

在iOS 7中,当呈现UIActionSheet时,系统控制所有将其tintColor为灰色的animation。 当表被解雇时,他们回生。

我有一些使用自定义背景图像或使用tint颜色的drawRect实现的控件,我希望它们像系统控件一样为这个更改设置animation。

苹果添加- (void)tintColorDidChange UIView ,但在这种方法重新绘制与新的颜色不animation的变化 – 它只是立即从全彩色切换到全灰色,这看起来不好,当其他周围的系统控制是animation。

我怎样才能使我的自定义绘制的控制animation像苹果的tintColor过渡?

您可以使用应用于视图图层的Core Animation转换来实现此操作:

 //set up transition CATransition *transition = [CATransition animation]; transition.type = kCATransitionFade; transition.duration = 0.4; [self.layer addAnimation:transition forKey:nil]; //now trigger a redraw of the background using the new colour //and the transition will cover the redraw with a crossfade self.flagToIndicateColorShouldChange = YES; [self setNeedsDisplay]; 

编辑:可能有animation和重绘之间的竞争条件,具体取决于你如何做重绘。 如果你可以发布原始的重绘代码,你尝试立即更新(没有animation),我可以提供一个更具体的答案。

不应drawRect:为animation更新为新的tintColor自动调用?

我做了一个演示应用程序,在主视图控制器中有三个控件。 第一个是提供标准操作表的button。 第二个是在那里观察的button(在animation过程中点击的button很难比较)。 第三个是自定义的UIView子类,它只是绘制视图的tintColor的矩形。 当调用tintColorDidChange ,我调用了setNeedsDisplay ,它将调用drawRect:

我用一个视图控制器创build了一个新应用程序:

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [[UIApplication sharedApplication] keyWindow].tintColor = [UIColor blueColor]; // Button to bring up action sheet UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = (CGRect){10,30,300,44}; [button setTitle:@"Present Action Sheet" forState:UIControlStateNormal]; [button addTarget:self action:@selector(didTapPresentActionSheetButton:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; // Another button for demonstration UIButton *anotherButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; anotherButton.frame = (CGRect){10,90,300,44}; [anotherButton setTitle:@"Another Button" forState:UIControlStateNormal]; [self.view addSubview:anotherButton]; // Custom view with tintColor TESTCustomView *customView = [[TESTCustomView alloc] initWithFrame:(CGRect){10,150,300,44}]; [self.view addSubview:customView]; } - (void)didTapPresentActionSheetButton:(id)sender { UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"Action Sheet" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"Other", nil]; [as showInView:self.view]; } 

其中TESTCustomView是一个UIView子类,具体实现如下:

 - (void)drawRect:(CGRect)rect { NSLog(@"Drawing with tintColor: %@", self.tintColor); // Drawing code [super drawRect:rect]; CGContextRef c = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(c, self.tintColor.CGColor); CGContextFillRect(c, rect); } - (void)tintColorDidChange { [self setNeedsDisplay]; } 

在模拟器中运行此应用程序显示自定义视图的tintColor自动使用视图控制器中的标准UIButton实例进行animation。

你试过tintAdjustmentMode吗?

您应该观看WWDC 2013 Session 214自定义iOS 7的应用外观,并阅读TicTacToe演示应用

像这样的东西

 - (void)onPopupOpened { [UIView animateWithDuration:0.3 animations:^{ window.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed; }]; popupView.tintAdjustmentMode = UIViewTintAdjustmentModeNormal; popupView.tintColor = [UIColor redColor]; } - (void)onPopupClosed { [UIView animateWithDuration:0.3 animations:^{ window.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic; }]; }