版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.01.21 |
前言
在蘋果的API文檔中,有很多屬性和方法我們用的不是很多满俗,所以很容易忽略和出錯转捕,下面我就用這一個專題專門說一些不常用的API接口,下面開始唆垃。
UIViewTintAdjustmentMode
這是一個枚舉值五芝,一般很少用到,是iOS 7.0才出現(xiàn)的辕万,下面先看一下這個枚舉枢步。
typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {
UIViewTintAdjustmentModeAutomatic,
UIViewTintAdjustmentModeNormal,
UIViewTintAdjustmentModeDimmed,
} NS_ENUM_AVAILABLE_IOS(7_0);
1. 作用
我們先看一個與該枚舉相關(guān)的屬性。
/*
-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);
tintAdjustmentMode總是返回UIViewTintAdjustmentModeNormal或UIViewTintAdjustmentModeDimmed渐尿。 返回的值
是接收者的超視圖鏈中的第一個非默認(rèn)值(從自身開始)醉途。 如果沒有找到非默認(rèn)值,則返回UIViewTintAdjustmentModeNormal砖茸。
當(dāng)tintAdjustmentMode對于視圖的值為UIViewTintAdjustmentModeDimmed時隘擎,它將從tintColor返回的顏色將被修改為變暗的外觀。
當(dāng)視圖的tintAdjustmentMode改變時(視圖的值改變或者它的一個超視圖值改變)凉夯,將調(diào)用-tintColorDidChange來允許視圖刷新其呈現(xiàn)货葬。
下面我們就說一下該枚舉值的作用采幌,在說明這個枚舉值之前,我們先要看一個屬性震桶,它也是iOS 7.0出現(xiàn)的休傍,定義在UIView的分類UIViewRendering
中。
/*
-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.
tintColor總是返回一個顏色尼夺。 返回的顏色是接收者父視圖鏈中的第一個非默認(rèn)值(從自身開始)尊残。 如果未找到非默認(rèn)值,則返回系統(tǒng)定義的顏色淤堵。 如果此視圖的
-tintAdjustmentMode返回Dimmed寝衫,則為-tintColor返回的顏色將自動變暗。 如果您的視圖子類在其渲染中使用tintColor拐邪,請覆蓋-tintColorDidChange
以便在顏色更改時刷新渲染慰毅。
*/
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(7_0);
tintColor
這個屬性定義了一個非默認(rèn)的著色顏色值,其值的設(shè)置會影響到以視圖為根視圖的整個視圖層次結(jié)構(gòu)扎阶。它主要是應(yīng)用到諸如app圖標(biāo)汹胃、導(dǎo)航欄、按鈕等一些控件上东臀,以獲取一些有意思的視覺效果着饥。
默認(rèn)情況下,一個視圖的tintColor
是為nil的惰赋,這意味著視圖將使用父視圖的tint color值宰掉。當(dāng)我們指定了一個視圖的tintColor后,這個色值會自動傳播到視圖層次結(jié)構(gòu)(以當(dāng)前視圖為根視圖)中所有的子視圖上赁濒。如果系統(tǒng)在視圖層次結(jié)構(gòu)中沒有找到一個非默認(rèn)的tintColor值轨奄,則會使用系統(tǒng)定義的顏色值(藍(lán)色,RGB值為[0,0.478431,1]
拒炎,我們可以在IB中看到這個顏色)挪拟。因此,這個值總是會返回一個顏色值击你,即我們沒有指定它玉组。
當(dāng)tintAdjustmentMode
屬性設(shè)置為Dimmed
時,tintColor
的顏色值會自動變暗丁侄。而如果我們在視圖層次結(jié)構(gòu)中沒有找到默認(rèn)值球切,則該值默認(rèn)是Normal。
下面我們還需要看一個方法绒障,這個方法在分類UIViewRendering
中。
/*
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.
-當(dāng)其tintColor由客戶端代碼改變時捍歪,tintColorDidChange消息被發(fā)送到視圖
的適當(dāng)?shù)淖右晥D户辱;當(dāng)視圖的superview或tintAdjustmentMode更改時鸵钝,
其子視圖的tintColor被隱式地改變,也會受到tintColorDidChange消息庐镐。
*/
- (void)tintColorDidChange NS_AVAILABLE_IOS(7_0);
2. 實例驗證
下面我們就看一下示例驗證恩商。
1)系統(tǒng)默認(rèn)值
我們在自定義視圖中不加入任何控件,先打印出系統(tǒng)默認(rèn)的tintColor
和tintAdjustmentMode
必逆,看代碼
#import "JJCustomView.h"
@implementation JJCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
NSLog(@"self.view.tintColor = %@", self.tintColor);
NSLog(@"self.view.tintAdjustmentMode = %ld", self.tintAdjustmentMode);
}
return self;
}
- (void)tintColorDidChange
{
NSLog(@"tintColor或者tintAdjustmentMode更改了");
}
@end
下面看輸出結(jié)果
2018-01-21 11:11:43.590710+0800 JJLayer_demo1[1442:123655] self.view.tintColor = UIExtendedSRGBColorSpace 0 0.478431 1 1
2018-01-21 11:11:43.590906+0800 JJLayer_demo1[1442:123655] self.view.tintAdjustmentMode = 1
從這里我們可以看見怠堪,系統(tǒng)默認(rèn)的tintColor是UIExtendedSRGBColorSpace 0 0.478431 1 1
,默認(rèn)的tintAdjustmentMode
是1名眉,也就是UIViewTintAdjustmentModeNormal
粟矿。
2)加控件后的系統(tǒng)默認(rèn)值
我們還是看代碼
#import "JJCustomView.h"
@implementation JJCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
NSLog(@"self.view.tintColor = %@", self.tintColor);
NSLog(@"self.view.tintAdjustmentMode = %ld", self.tintAdjustmentMode);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = self.tintColor;
[self addSubview:button];
UILabel *label = [[UILabel alloc] init];
label.text = @"tintColor";
label.frame = CGRectMake(100, 220, 100, 40);
label.backgroundColor = self.tintColor;
[self addSubview:label];
}
return self;
}
- (void)tintColorDidChange
{
NSLog(@"tintColor或者tintAdjustmentMode更改了");
}
@end
這里我們將button和label的背景色都設(shè)置為self.tintColor
,下面看一下界面效果圖损拢。
3)加控件后的系統(tǒng)Dim樣式
我們還是先看一下代碼陌粹。
#import "JJCustomView.h"
@implementation JJCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
NSLog(@"self.view.tintColor = %@", self.tintColor);
NSLog(@"self.view.tintAdjustmentMode = %ld", self.tintAdjustmentMode);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = self.tintColor;
[self addSubview:button];
UILabel *label = [[UILabel alloc] init];
label.text = @"tintColor";
label.frame = CGRectMake(100, 220, 100, 40);
label.backgroundColor = self.tintColor;
[self addSubview:label];
}
return self;
}
- (void)tintColorDidChange
{
NSLog(@"tintColor或者tintAdjustmentMode更改了");
}
@end
下面看輸出結(jié)果
2018-01-21 11:35:53.752995+0800 JJLayer_demo1[1621:152807] tintColor或者tintAdjustmentMode更改了
2018-01-21 11:35:53.754146+0800 JJLayer_demo1[1621:152807] self.view.tintColor = UIExtendedGrayColorSpace 0.484669 0.8
2018-01-21 11:35:53.754314+0800 JJLayer_demo1[1621:152807] self.view.tintAdjustmentMode = 2
下面看一下界面效果
大家可以看見,dim樣式的tintColor值是UIExtendedGrayColorSpace 0.484669 0.8
福压,變得更深色了掏秩。
4) 加控件后修改tintColor效果
下面還是看一下代碼
#import "JJCustomView.h"
@implementation JJCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.tintColor = [UIColor redColor];
NSLog(@"self.view.tintColor = %@", self.tintColor);
NSLog(@"self.view.tintAdjustmentMode = %ld", self.tintAdjustmentMode);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 100);
button.backgroundColor = self.tintColor;
[self addSubview:button];
UILabel *label = [[UILabel alloc] init];
label.text = @"tintColor";
label.frame = CGRectMake(100, 220, 100, 40);
label.backgroundColor = self.tintColor;
[self addSubview:label];
}
return self;
}
- (void)tintColorDidChange
{
NSLog(@"tintColor或者tintAdjustmentMode更改了");
}
@end
下面看輸出結(jié)果
2018-01-21 11:38:01.505139+0800 JJLayer_demo1[1646:155386] tintColor或者tintAdjustmentMode更改了
2018-01-21 11:38:01.505865+0800 JJLayer_demo1[1646:155386] self.view.tintColor = UIExtendedSRGBColorSpace 1 0 0 1
2018-01-21 11:38:01.506054+0800 JJLayer_demo1[1646:155386] self.view.tintAdjustmentMode = 1
輸出的意思很明確,就不多說了荆姆,直接看一下顯示效果蒙幻。
應(yīng)用場景舉例
如果我們想指定整個App的
tint color
,則可以通過設(shè)置window的tint color胆筒。這樣同一個window下的所有子視圖都會繼承此tint color邮破。當(dāng)彈出一個alert或者action sheet時,iOS7會自動將后面視圖的tint color變暗腐泻。此時决乎,我們可以在自定義視圖中重寫
tintColorDidChange
方法來執(zhí)行我們想要的操作。
參考文章
1. 詳解 UIView 的 Tint Color 屬性
2. Hues, Tints, Tones and Shades: What’s the Difference?
3. iOS7 Day-by-Day :: Day 6 :: Tint Color