2009-08-31 15 views
0

我一直想在我的iPhone應用程序中製作圓形預加載器,但一直在努力從哪裏開始。我今天正在使用Sequel Pro,看到他們正是我想要的,而且它是開源的,所以我下載了源代碼並找不到任何東西。目標C中的圓形預加載器

我只是在尋找如何使沿線的東西開始:

alt text http://www.grabup.com/uploads/c7b2d101cd9caf0d927c2fa8a7850ba2.png

alt text http://www.grabup.com/uploads/fc8520a4425331a3a6a0cbf6508b510f.png

我發現了一些Flash教程,但很難將它翻譯成Objective-C中可用的代碼。

在此先感謝。

回答

3

爲此目的創建一個UIView子類並不困難。在子類中,你會想要做大致在drawRect程序如下:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGFloat  center_x = self.frame.size.width/2; 
CGFloat  center_y = self.frame.size.height/2; 
double  progress = 0.42; // A floating-point number from 0..1 inclusively 

// draw the frame 
CGContextAddArc(context, 
       center_x, 
       center_y, 
       std::min(self.frame.size.width, self.frame.size.height), 
       0, 
       M_PI * 2, 
       1 /*clockwise*/); 
CGContextStrokePath(context); 

// draw the progress indicator 
CGContextAddArc(context, 
       center_x, 
       center_y, 
       std::min(self.frame.size.width, self.frame.size.height), 
       0, 
       M_PI * 2 * progress, 
       1 /*clockwise*/); 
CGContextFillPath(context); 
+0

小問題:那些CGPoint變量應該聲明爲CGFloat。 – Don 2009-09-02 19:23:20

0

你的意思是進度指示器,比如在Interface Builder,但圓形包括一個?我假設在Flex框架中有一個默認的你已經看過的教程。

我想你必須自己做這個。你可以做的是有一個視圖,用於偵聽來自對象的進度通知。當它接收到這些信息時,您將計算要顯示的圖像並調用viewNeedsDisplay方法。

我快速瀏覽了Sequel Pro的源代碼,我想你要找的是NSProgressIndicator。但Sequel Pro是一款桌面應用程序,比iPhone更具靈活性。 Cocoa Touch等價物將是UIActivityIndi​​catorView,它始終是不確定的(即,您無法指定已完成的百分比)。

編輯:是的,請參閱以上fbrereton代碼的繪圖程序。

這裏是你如何會聽的通知來獲取進步浮點值:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyProgressIndication" 
                 object:[NSNumber numberFromFloat:progress]]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(receivedProgress:) 
               name:@"MyProgressIndication" 
               object:nil]; 

你會從正在做的工作是這樣的對象張貼

通知名稱的常量是最好的。

1

您應該使用AnimatedGif庫,並從preloaders.net或其他地方下載動畫GIF。

只是以編程方式進行(沒有IB)。這很簡單。下面的代碼:

AnimatedGif *animatedGif = [[[AnimatedGif alloc] init] autorelease];  
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"circle-loader" ofType:@"gif"]]; 
[animatedGif decodeGIF: data]; 
self.loadingImage = [animatedGif getAnimation]; //self.loadingImage is a UIImageView 
[self.view addSubview:loadingImage]; 

然後,以後當你要刪除的GIF:

[loadingImage removeFromSuperview]; 
[loadingImage release];