自定义UIProgressView与圆角图像

时间:2013-02-08 14:59:06

标签: ios objective-c ios6 uiprogressview

我无法使用两张图片自定义UIProgressView。我在互联网上找到了很多有用的问题,但我的问题有点不同。也许在某种程度上,我使用的图像是圆形的,没有角落的矩形;因此,stretchableImageWithLeftCapWidth方法似乎对我的情况没有帮助。当我拉伸图像时,由于图像的圆角存在一些空间,因此在link上发布了一个问题

2 个答案:

答案 0 :(得分: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&amp ;;设置自定义值。 heightofProgressView 根据您的要求

答案 1 :(得分:1)

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

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

You can check out this link

相关问题