2012-07-10 79 views
0

我想通過繼承創建自己的自定義UIProgressView,然後覆蓋drawRect函數。 除了進度填充欄之外,一切都按預期工作。我無法獲得正確的身高和形象。自定義UIProgressView繪圖怪異

圖像都是視網膜分辨率,模擬器是視網膜模式。 圖像被稱爲:「[email protected]」(28px高)和「[email protected]」(高32px)。

CustomProgressView.h

#import <UIKit/UIKit.h> 

@interface CustomProgressView : UIProgressView 

@end 

CustomProgressView.m

#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 
{ 
    // Drawing code 
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 16); 

    UIImage *progressBarTrack = [[UIImage imageNamed:@"progressBarTrack"] resizableImageWithCapInsets:UIEdgeInsetsZero]; 
    UIImage *progressBar = [[UIImage imageNamed:@"progressBar"] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 5, 4)]; 

    [progressBarTrack drawInRect:rect]; 

    NSInteger maximumWidth = rect.size.width - 2; 
    NSInteger currentWidth = floor([self progress] * maximumWidth); 

    CGRect fillRect = CGRectMake(rect.origin.x + 1, rect.origin.y + 1, currentWidth, 14); 

    [progressBar drawInRect:fillRect]; 
} 

@end 

所得ProgressView有權高度和寬度。它也填寫正確的比例(目前定爲80%)。但進度填充圖像繪製不正確。

有沒有人看到我出錯的地方?

Screenshot to show the problem

回答

2

你好像在-drawRect重新分配self.frame

我想你想是這樣的:

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

    UIImage *progressBarTrack = [ UIImage imageNamed:@"progressBarTrack"] ; 
    [ progressBarTrack drawInRect:bounds ] ; 

    UIImage *progressBar = [[UIImage imageNamed:@"progressBar"] resizableImageWithCapInsets:(const UIEdgeInsets){ 4.0f, 4.0f, 5.0f, 4.0f } ] ; 

    CGRect fillRect = CGRectInset(bounds, 2.0f, 2.0f) ; 
    fillRect.width = floorf(self.progress * maximumWidth); 

    [progressBar drawInRect:fillRect]; 
} 

如何創建自己的發展觀覆蓋的UIView而不是UIProgressView

@interface ProgressView : UIView 
@property float progress ; 
@end 

@implementation ProgressView 
@synthesize progress = _progress ; 

-(id)initWithFrame:(CGRect)frame 
{ 
    if ((self = [ super initWithFrame:frame ])) 
    { 
     self.layer.needsDisplayOnBoundsChange = YES ; 
    } 

    return self ; 
} 

-(void)drawRect 
{ 
    // see code above 
} 

-(void)setProgress:(float)progress 
{ 
    _progress = progress ; 
    [ self setNeedsDisplay ] ; 
} 

@end 
+0

我這樣做是爲了使ProgressView 16點高,而不是的默認9分。在你的例子中,我找不到它。 – Werner 2012-07-10 15:23:28

+0

哦 - 對。儘管如此,你不應該那樣做。我會將'clipsToBounds'設置爲'NO',或者將子類UIView'而不是'UIProgressView'。 – nielsbot 2012-07-10 15:24:54

+0

即你正在對抗UIProgresssView的內置實現。在這些情況下,我只是自己做。此外,如果他們改變了有關它們的實現等等,你可能會在未來的iOS上遇到麻煩等。 – nielsbot 2012-07-10 15:25:48