2012-12-05 33 views
1

這是我做過什麼:子類UIActivityIndi​​cator改變形象

@implementation BGUIActivityIndicator 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    [self customInitialize]; 
    return self; 
} 

-(id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 
    [self customInitialize]; 
    return self; 
} 

-(void)customInitialize 
{ 
    UIView * theImageView= [self findASubViewforClass:[UIImageView class]];// 
    UIImageView * theImageView1 = (UIImageView *) theImageView; 
    theImageView1.image = [UIImage imageNamed:@"spinner_blue"]; 
    [theImageView1.image saveScreenshot]; 
    while (false) ; 
} 
/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 

一切都顯得完美。 [theImageView1.image saveScreenshot];完美地記錄新舊觀點。

然而,沒有什麼變化。爲什麼?

我不完全問如何更改UIActivityIndi​​cator的圖像。已經有很多了。我想通過繼承UIActivityIndi​​cator來使用它,因爲我認爲這是最優雅的解決方案。我似乎無法做到這一點。

特別是,我在問爲什麼我的方法,例如改變搜索控制器的背景,不適用於此。

回答

2

根據UIActivityIndicatorView Class Reference,沒有方法/機會通過子分類來改變圖像。

但是你可以改變它的activityIndi​​catorViewStyle,活動指標,UIActivityIndi​​catorStyle等顏色..

我認爲,沒有子類,類類的UIImageView提供了實現一個非常有用的和簡單的方法這樣的事情。你所要做的唯一一件事情就是:

1.Provide a number of images that reflect your indicator animation. 
2.Create a new UIImageView instance and set images and animation duration. 
3.Position your custom activity indicator within your current view. 

示例代碼:

//Create the first status image and the indicator view 
UIImage *statusImage = [UIImage imageNamed:@"status1.png"]; 
UIImageView *activityImageView = [[UIImageView alloc] 
       initWithImage:statusImage]; 


//Add more images which will be used for the animation 
activityImageView.animationImages = [NSArray arrayWithObjects: 
      [UIImage imageNamed:@"status1.png"], 
      [UIImage imageNamed:@"status2.png"], 
      [UIImage imageNamed:@"status3.png"], 
      [UIImage imageNamed:@"status4.png"], 
      [UIImage imageNamed:@"status5.png"], 
      [UIImage imageNamed:@"status6.png"], 
      [UIImage imageNamed:@"status7.png"], 
      [UIImage imageNamed:@"status8.png"], 
      nil]; 


//Set the duration of the animation (play with it 
//until it looks nice for you) 
activityImageView.animationDuration = 0.8; 


//Position the activity image view somewhere in 
//the middle of your current view 
activityImageView.frame = CGRectMake(
      self.view.frame.size.width/2 
       -statusImage.size.width/2, 
      self.view.frame.size.height/2 
       -statusImage.size.height/2, 
      statusImage.size.width, 
      statusImage.size.height); 

//Start the animation 
[activityImageView startAnimating]; 


//Add your custom activity indicator to your current view 
[self.view addSubview:activityImageView]; 

查看完整的細節Here

相關問題