2014-07-20 42 views
0

我目前試圖創建一個簡單的方法調用我的ViewController如:iOS:有沒有辦法獲得視圖中每個元素的backgroundColor?

[self greyscale]; 

凡灰階處於UIViewController類的方法。使用遞歸和塊,我試圖收集每一個元素在ViewController並得到他們的backgroundColor屬性,然後改變這些值,使之成爲灰度什麼,我試圖做的是即[UIColor colorWithWhite:(0.299*red + 0.587*green + 0.114*blue) alpha:alpha];

我已經試過了以下(到目前爲止只存在了uibuttons):

- (void)runBlockOnAllSubviews:(SubviewBlock)block { 

    block(self.view); 
    for (UIViewController* viewController in [self.view subviews]) { 
     [viewController runBlockOnAllSubviews:block]; 
    } 
} 

- (void)greyscale { 

    [self runBlockOnAllSubviews:^(UIView *view) { 

    if ([view isKindOfClass:[UIButton class]]) { 
     [(UIControl *)view setBackgroundColor:[UIColor redColor]]; 
     [(UIButton *)view setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; 
    } 
}]; 

到目前爲止設置背景色的作品,但問題是我已經接收元件的電流的backgroundColor所以我可以把它送到一個具有「灰度化的另一種方法「對我來說,所以我可以做類似的事情:

[(UIControl *)view setBackgroundColor:[UIColor greyScaleWith:view.backgroundColor]]; 

當我嘗試將其設置爲白色時,因爲"view"'s backgroundColornil0;

有沒有更好的方式來訪問和更改uiview上的每個元素的顏色,而不會使它在初始ViewController中變得複雜?我想這樣做灰度,darkerShade等。

謝謝!

+0

你似乎這裏有兩個問題。每個主題詢問一個。你需要幫助你爲什麼變白?還是你想幫助完成這個整個過程? – rmaddy

+0

如果這不是我想做的最理想的方式,我很想學習如何更有效地做到這一點。 – KingPolygon

回答

1

目前還不清楚你在代碼塊中要做什麼;我認爲使用塊來完成您的任務沒有任何特別的優勢。此外,你的第一個循環(for(UIViewController * viewController in [self.view subviews]))沒有意義,因爲沒有視圖控制器作爲視圖的子視圖。您可以在UIView的類別中使用簡單的遞歸方法更改所有顏色。我已經包含了一個typedef來定義不同類型的顏色轉換。我的示例只是將顏色轉換爲淺灰色或灰色,因此您應該使用convertColorForView:conversionType:中的任何算法來替代您的目標。這裏的.H,

typedef NS_ENUM(NSInteger, RDColorConversionType) { 
    RDColorConversionTypeGrayScale = 0, 
    RDColorConversionTypeDarken, 
}; 

@interface UIView (ColorConversions) 

- (void)convertSubviewsBackgroundColor:(RDColorConversionType) type; 

.m文件,

@implementation UIView (ColorConversions) 

- (void)convertSubviewsBackgroundColor:(RDColorConversionType) type { 
    //[self grayScaleForView:self]; 
    for (UIView *subview in self.subviews) { 
     [self convertColorForView:subview conversionType:type]; // delete this line, and uncomment the one above if you want the view that calls this method to have its background color changed too. 
     [subview convertSubviewsBackgroundColor:type]; 
    } 
} 


-(void)convertColorForView:(UIView *) view conversionType:(RDColorConversionType) type { 
    switch (type) { 
     case RDColorConversionTypeGrayScale: 
      view.backgroundColor = [UIColor lightGrayColor]; 
      break; 
     case RDColorConversionTypeDarken: 
      view.backgroundColor = [UIColor grayColor]; 
      break; 
    } 

} 

你會調用這個方法在您的視圖控制器像這樣,

[self.view convertSubviewsBackgroundColor:RDColorConversionTypeGrayScale]; 
+0

感謝您的rdelmar!我想我只是試圖從viewController調用這個方法,因爲我也喜歡從該視圖控制器的父級(如果存在)訪問導航欄。我怎樣才能使用它來訪問視圖元素的父級? – KingPolygon

+0

@KingPolygon,我從視圖控制器調用該方法。我不確定你是什麼意思的「視圖元素的父母」。 – rdelmar

+0

抱歉,我的意思是調用viewController本身的方法,比如[self converSubviews ...],這樣我也可以檢查ViewController本身是否有一個navigationController作爲它的父視圖,並且還調整了background/bar的tint屬性導航欄。 – KingPolygon

相關問題