首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在iOS 7中显示工作表/警报时,如何在自定义绘制的控件上将tintColor动画化为灰色?

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

Stack Overflow用户
提问于 2013-11-06 04:51:27
回答 3查看 9.9K关注 0票数 34

在iOS 7中,当显示UIActionSheet时,系统会将所有控件的tintColor动画设置为灰色。当图纸关闭时,它们会重新显示动画。

我有一些带有自定义背景图像或使用色调颜色的drawRect实现的控件,我希望它们能像系统控件一样为这种变化提供动画效果。

苹果在UIView中添加了- (void)tintColorDidChange,但用这种方法重新绘制新的颜色并不会使变化的- it立即从全色切换到全灰,当周围的其他系统控件正在进行动画处理时,这看起来很糟糕。

我怎样才能让我的自定义绘制控件像苹果一样为tintColor过渡设置动画效果呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-06 05:05:51

可以使用应用于视图层的核心动画过渡来执行此操作:

代码语言:javascript
运行
复制
//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];

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

票数 28
EN

Stack Overflow用户

发布于 2014-02-24 14:19:51

你试过tintAdjustmentMode吗?

您应该观看WWDC 2013 Session 214 Customizing Your App’s Appearance for iOS 7并阅读TicTacToe demo app

像这样的东西

代码语言:javascript
运行
复制
- (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;
    }];
}
票数 7
EN

Stack Overflow用户

发布于 2013-11-06 05:39:35

是否应该为动画自动调用drawRect:以针对新的tintColor进行更新

我已经制作了一个演示应用程序,在主视图控制器中有三个控件。第一个是一个按钮,它显示了一个标准的操作表。第二个是一个用于观察的按钮(在动画过程中很难与点击的按钮进行比较)。第三个是一个定制的UIView子类,它只是绘制视图的tintColor的一个矩形。当调用tintColorDidChange时,我调用setNeedsDisplay,后者将调用drawRect:

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

代码语言:javascript
运行
复制
- (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子类,其实现如下:

代码语言:javascript
运行
复制
- (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实例自动生成动画的。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19798712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档