gpt4 book ai didi

ios - 带有圆角图像的自定义 UIProgressView

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:11:38 24 4
gpt4 key购买 nike

我在使用两个图像自定义 UIProgressView 时遇到困难。我在互联网上发现了很多有用的问题,但我的问题有点不同。也许在某种程度上,我正在使用的图像在没有角的矩形上是圆的;因此,stretchableImageWithLeftCapWidth 方法似乎对我的情况没有帮助。当我拉伸(stretch)图像时,由于图像的圆角,有一些空间,这里有一个问题 link

最佳答案

您必须实现自定义 UiProgressView 并将以下代码用于您的进度 View 并添加您的图像以填充和背景进度 View ,在我的例子中它们是 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

最后当你想使用自定义进度 View 时调用这样的东西......

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

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

关于ios - 带有圆角图像的自定义 UIProgressView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com