uikit——UIView——tintColor tintAdjustmentMode

tintColor&tintAdjustmentMode

/*
 -tintColor always returns a color. The color returned is the first non-default value in the receiver's superview chain (starting with itself).
 If no non-default value is found, a system-defined color is returned.
 If this view's -tintAdjustmentMode returns Dimmed, then the color that is returned for -tintColor will automatically be dimmed.
 If your view subclass uses tintColor in its rendering, override -tintColorDidChange in order to refresh the rendering if the color changes.
 */
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(7_0);

/*
 -tintAdjustmentMode always returns either UIViewTintAdjustmentModeNormal or UIViewTintAdjustmentModeDimmed. The value returned is the first non-default value in the receiver's superview chain (starting with itself).
 If no non-default value is found, UIViewTintAdjustmentModeNormal is returned.
 When tintAdjustmentMode has a value of UIViewTintAdjustmentModeDimmed for a view, the color it returns from tintColor will be modified to give a dimmed appearance.
 When the tintAdjustmentMode of a view changes (either the view's value changing or by one of its superview's values changing), -tintColorDidChange will be called to allow the view to refresh its rendering.
 */
@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode NS_AVAILABLE_IOS(7_0);

/*
 The -tintColorDidChange message is sent to appropriate subviews of a view when its tintColor is changed by client code or to subviews in the view hierarchy of a view whose tintColor is implicitly changed when its superview or tintAdjustmentMode changes.
 */
- (void)tintColorDidChange NS_AVAILABLE_IOS(7_0);
typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {
    UIViewTintAdjustmentModeAutomatic,
    
    UIViewTintAdjustmentModeNormal,
    UIViewTintAdjustmentModeDimmed,
} NS_ENUM_AVAILABLE_IOS(7_0);
解释:
  • tintColor默认为系统定义颜色,tintColor值改变,更新view hierarchy的tintColor值,并调用tintColorDidChange
  • tintAdjustmentMode默认为UIViewTintAdjustmentModeNormal,tintAdjustmentMode值改变,更新view hierarchy的tintAdjustmentMode值,并调用tintColorDidChange
  • tintAdjustmentMode值为UIViewTintAdjustmentModeDimmed时,会对tintColor进行灰色处理
  • tintColor不支持pattern image,如果tintColor为pattern image,throw exception

应用

@interface FBView : UIView

@end

@implementation FBView

- (void)tintColorDidChange
{
    NSLog(@"tintColorDidChange: tag = %ld, tintColor = %@, tintAdjustmentMode = %lu", self.tag, self.tintColor, self.tintAdjustmentMode);
}

@end
- (void)tint
{
    FBView *view = [[FBView alloc] initWithFrame:CGRectZero];
    FBView *view1 = [[FBView alloc] initWithFrame:CGRectZero];
    FBView *view2 = [[FBView alloc] initWithFrame:CGRectZero];
    FBView *view1_1 = [[FBView alloc] initWithFrame:CGRectZero];
    FBView *view1_2 = [[FBView alloc] initWithFrame:CGRectZero];
    
    view.tag = 5;
    view1.tag = 1;
    view2.tag = 2;
    view1_1.tag = 11;
    view1_2.tag = 12;
    
    [self.view addSubview:view];
    [view addSubview:view1];
    [view addSubview:view2];
    [view1 addSubview:view1_1];
    [view1 addSubview:view1_2];
    
    NSLog(@"default: tag = %ld, tintColor = %@, tintAdjustmentMode = %lu", view.tag, view.tintColor, view.tintAdjustmentMode);
    NSLog(@"change tintColor");
    view.tintColor = [UIColor blueColor];
    NSLog(@"change tintAdjustmentMode");
    view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
}
output:
default: tag = 5, tintColor = UIDeviceRGBColorSpace 0 0.478431 1 1, tintAdjustmentMode = 1
change tintColor
tintColorDidChange: tag = 5, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
tintColorDidChange: tag = 1, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
tintColorDidChange: tag = 11, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
tintColorDidChange: tag = 12, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
tintColorDidChange: tag = 2, tintColor = UIDeviceRGBColorSpace 0 0 1 1, tintAdjustmentMode = 1
change tintAdjustmentMode
tintColorDidChange: tag = 5, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
tintColorDidChange: tag = 1, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
tintColorDidChange: tag = 11, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
tintColorDidChange: tag = 12, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2
tintColorDidChange: tag = 2, tintColor = UIDeviceWhiteColorSpace 0.11 0.8, tintAdjustmentMode = 2

你可能感兴趣的:(uikit)