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

Mar*_*rco 34 iphone core-animation uikit ios ios7

在iOS 7中,当UIActionSheet呈现a时,系统控制所有动画将它们设置tintColor为灰色.当纸张被解散时,它们会动画回来.

我有一些使用自定义背景图像或drawRect使用色调颜色的实现的控件,我希望它们像系统控件一样为该更改设置动画.

Apple添加- (void)tintColorDidChangeUIView,但是在这种方法中使用新颜色重新绘制并不会使更改生动 - 它只是立即从全彩切换到全灰色,当它周围的其他系统控件生成动画时看起来很糟糕.

如何使自定义绘制的控件tintColor像Apple一样动画转换?

Nic*_*ood 28

您可以通过应用于视图层的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];
Run Code Online (Sandbox Code Playgroud)

编辑:动画和重绘之间可能存在竞争条件,具体取决于您如何进行重绘.如果您可以发布您尝试立即更新的原始重绘代码(没有动画),我可以提供更具体的答案.

  • 我不相信它有一个常数.默认的CoreAnimation或UIView动画持续时间为0.25秒,但Apple经常使用较长的持续时间,如0.3或0.33,因此您可能只需要尝试查看看起来正确的内容. (5认同)

onm*_*133 7

你试过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;
    }];
}
Run Code Online (Sandbox Code Playgroud)


小智 6

是不是drawRect:应该自动调用动画来更新新的tintColor

我做了一个演示应用程序,在主视图控制器中有三个控件.第一个是打开标准操作表的按钮.第二个是用于观察的按钮(点击按钮很难与动画期间进行比较).第三个是自定义UIView子类,它只是绘制视图的矩形tintColor.当tintColorDidChange被叫时,我打电话setNeedsDisplay,然后打电话drawRect:.

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

- (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];
}
Run Code Online (Sandbox Code Playgroud)

where 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];
}
Run Code Online (Sandbox Code Playgroud)

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