0

サブクラス化して独自のカスタムUIProgressViewを作成し、drawRect関数を上書きしようとしています。進行状況入力バーを除いて、すべてが期待どおりに機能します。高さと画像が正しくわかりません。

画像は両方ともRetina解像度であり、シミュレーターはRetinaモードです。画像の名前は「progressBar@2x.png」(高さ28px)および「progressBarTrack@2x.png」(高さ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%に設定されています)。ただし、進行状況の塗りつぶし画像は正しく描画されません。

誰かが私がどこで間違っているのかわかりますか?

問題を示すスクリーンショット

4

1 に答える 1

2

で再割り当てself.frameしているようです-drawRect

私はあなたがこのようなものが欲しいと思います:

// 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];
}

UIProgressViewの代わりにUIViewをオーバーライドする独自の進行状況ビューを作成する方法

@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
于 2012-07-10T15:17:20.133 に答える