在iOS 7中,当显示UIActionSheet
时,系统会将所有控件的tintColor
动画设置为灰色。当图纸关闭时,它们会重新显示动画。
我有一些带有自定义背景图像或使用色调颜色的drawRect
实现的控件,我希望它们能像系统控件一样为这种变化提供动画效果。
苹果在UIView
中添加了- (void)tintColorDidChange
,但用这种方法重新绘制新的颜色并不会使变化的- it立即从全色切换到全灰,当周围的其他系统控件正在进行动画处理时,这看起来很糟糕。
我怎样才能让我的自定义绘制控件像苹果一样为tintColor
过渡设置动画效果呢?
发布于 2013-11-06 05:05:51
可以使用应用于视图层的核心动画过渡来执行此操作:
//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];
编辑:动画和重绘之间可能存在争用条件,具体取决于执行重绘的方式。如果你可以发布你尝试的原始重绘代码,立即更新(没有动画),我可以提供更具体的答案。
发布于 2014-02-24 14:19:51
你试过tintAdjustmentMode
吗?
您应该观看WWDC 2013 Session 214 Customizing Your App’s Appearance for iOS 7并阅读TicTacToe demo app
像这样的东西
- (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;
}];
}
发布于 2013-11-06 05:39:35
是否应该为动画自动调用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];
}
其中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
实例自动生成动画的。
https://stackoverflow.com/questions/19798712
复制相似问题