2

我在自定义UIProgressView两个图像时遇到了困难。我在互联网上找到了很多有用的问题,但我的问题有点不同。也许以某种方式,我使用的图像在非角矩形上是圆角的;因此,stretchableImageWithLeftCapWidth在我的情况下,方法似乎没有帮助。当我拉伸图像时,由于图像的圆角存在一些空间,此链接上发布了一个问题

4

2 回答 2

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)];

确保 根据您的要求为xValue、yValue、widthofProgressView 和 heightofProgressView设置自定义值

于 2013-02-08T15:03:36.107 回答
1

如果您在拉伸图像时遇到问题,请使用调整大小resizableImageWithCapInsets:

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

您可以查看此链接

于 2013-02-08T15:06:27.450 回答