2013-02-08 21 views
2

我在使用兩張圖像自定義UIProgressView時遇到困難。我在互聯網上發現了很多有用的問題,但我的問題有點不同。也許在某種程度上,我正在使用的圖像是四捨五入的沒有角色的矩形;因此,stretchableImageWithLeftCapWidth方法似乎沒有幫助我的情況。當我拉伸圖像時,由於圖像的圓角有一些空間,因此有一個問題發佈在此link具有圓角圖像的自定義UIProgressView

回答

2

您將不得不執行自定義UiProgressView並使用以下代碼進行視圖並添加圖像填充和後臺進度視圖,在我的情況下,他們是loaderBar.png爲填充和timeLineBig.png爲背景。

CustomProgressView.h

#import <UIKit/UIKit.h> 

@interface CustomProgressView : UIProgressView 

@end 

CustomProgressView.m

#define fillOffsetX 2 
#define fillOffsetTopY 2 
#define fillOffsetBottomY 2 

#import "CustomProgressView.h" 

@implementation CustomProgressView 

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


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 

CGSize backgroundStretchPoints = {10, 10}, fillStretchPoints = {7, 7}; 

// Initialize the stretchable images. 
UIImage *background = [[UIImage imageNamed:@"timelineBig.png"] stretchableImageWithLeftCapWidth:backgroundStretchPoints.width 
topCapHeight:backgroundStretchPoints.height]; 

UIImage *fill = [[UIImage imageNamed:@"loaderBar.png"] stretchableImageWithLeftCapWidth:fillStretchPoints.width 
topCapHeight:fillStretchPoints.height]; 

// Draw the background in the current rect 
[background drawInRect:rect]; 

// Compute the max width in pixels for the fill. Max width being how 
// wide the fill should be at 100% progress. 
NSInteger maxWidth = rect.size.width - (2 * fillOffsetX); 

// Compute the width for the current progress value, 0.0 - 1.0 corresponding 
// to 0% and 100% respectively. 
NSInteger curWidth = floor([self progress] * maxWidth); 

// Create the rectangle for our fill image accounting for the position offsets, 
// 1 in the X direction and 1, 3 on the top and bottom for the Y. 
CGRect fillRect = CGRectMake(rect.origin.x + fillOffsetX, 
rect.origin.y + fillOffsetTopY, 
curWidth, 
rect.size.height - fillOffsetBottomY); 

// Draw the fill 
[fill drawInRect:fillRect]; 
} 



@end 

最後當你想使用的自定義進度視圖然後調用這樣的事情...

self.customProgressView=[[CustomProgressView alloc] initWithFrame:CGRectMake(xValue, yValue, widthofProgressView, heightofProgressView)]; 

確保您根據您的要求

+0

感謝您的回答,我會試一試並回復給您,如果我遇到問題 – sam46 2013-02-08 15:05:20

+1

似乎很有幫助,但仍然需要在圖像上對高度和寬度進行修復,但我認爲,因此,正如你在答覆中所說的,感謝100萬 – sam46 2013-02-08 15:15:24

+1

做到了! – sam46 2013-02-08 16:23:37

1

如果您有streching圖像使用問題爲x值,y值,widthofProgressView & heightofProgressView設置自定義的值調整resizableImageWithCapInsets:

UIImage *image = [baseImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5) resizingMode:UIImageResizingModeStretch]; 

You can check out this link