ios - Objective-C UIImage 凹角(又名自定义进度条)

标签 ios objective-c uiimage

我需要帮助以编程方式在 Objective-c 中创建此图像。凹半径是图像高度的一半。

Box with circle taken out, than left side taken off

我的目标是有一个进度条,其中左侧是图像,中间是包含 View 的背景颜色,右侧是可拉伸(stretch)图像。

A color bar with the shape mentioned above at both ends


编辑

@Rob 的解决方案是要走的路。但是,我一定是错误地实现了它。

我创建了头文件和实现文件,并将该类添加到现有的 UIView、progressBarView。 CustomProgress View 出现在 IB 中,一切看起来都很棒......

IB example

...但是当我尝试使用...设置进度值时

self.progressBarView.progress = 0.75;

...我收到 Property 'progress' not found on object of type 'UIView *' 语义警告。

最佳答案

而不是创建任何图像,我可能只是将 cornerRadius 应用于主进度 View 以及显示进度的 View 。因此,想象一下以下 View 层次结构:

enter image description here

主要进度 View 是后面的白色边框 View 。它有一个 subview ,progressSubview(上面以蓝色突出显示),它显示了到目前为止的进度(并且会随着我们更新 progress 属性而改变)。绿色和蓝色 View 只是固定大小的 progressSubview subview 显示为 progressSubview,剪辑其 subview ,改变大小。

应用角半径非常容易。通过避免任何图像或自定义 drawRect,我们可以根据需要为 progressSubview 的变化设置动画:

enter image description here

例如

//  CustomProgressView.h

@import UIKit;

NS_ASSUME_NONNULL_BEGIN

IB_DESIGNABLE
@interface CustomProgressView : UIView

@property (nonatomic) CGFloat progress;

@end

NS_ASSUME_NONNULL_END

//  CustomProgressView.m

#import "CustomProgressView.h"

@interface CustomProgressView ()

@property (nonatomic, weak) UIView *progressSubview;
@property (nonatomic, weak) UIView *greenView;
@property (nonatomic, weak) UIView *redView;

@end

@implementation CustomProgressView

- (instancetype)init {
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self configure];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self configure];
    }
    return self;
}

- (void)configure {
    UIView *subview = [[UIView alloc] init];
    subview.backgroundColor = [UIColor clearColor];
    subview.clipsToBounds = true;
    self.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.layer.borderWidth = 1;

    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    self.redView = redView;

    UIView *greenView = [[UIView alloc] init];
    greenView.backgroundColor = [UIColor greenColor];
    self.greenView = greenView;

    [self addSubview:subview];
    [subview addSubview:redView];
    [subview addSubview:greenView];

    self.progressSubview = subview;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    self.layer.cornerRadius = MIN(self.bounds.size.height, self.bounds.size.width) / (CGFloat)2.0;
    self.progressSubview.layer.cornerRadius = MIN(self.bounds.size.height, self.bounds.size.width) / (CGFloat)2.0;
    [self updateProgressSubview];

    self.redView.frame = self.bounds;

    CGRect rect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.origin.x + self.bounds.size.width / 2.0, self.bounds.origin.y + self.bounds.size.height);
    self.greenView.frame = rect;
}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    [self updateProgressSubview];
}

- (void)updateProgressSubview {
    CGRect rect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.origin.x + self.bounds.size.width * self.progress, self.bounds.origin.y + self.bounds.size.height);
    self.progressSubview.frame = rect;
}

- (void)prepareForInterfaceBuilder {
    [super prepareForInterfaceBuilder];

    self.progress = 0.75;
}

@end

然后你可以像这样更新进度:

[UIView animateWithDuration:0.25 animations:^{
    self.progressView.progress = 0.75;
}];

...I get a Property 'progress' not found on object of type 'UIView *' semantic warning

您的 IBOutlet 似乎被定义为 UIView 而不是 CustomProgressView

关于ios - Objective-C UIImage 凹角(又名自定义进度条),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54917352/

相关文章:

ios - containerURLForSecurityApplicationGroupIdentifier : gives different results on iPhone and Watch simulator

ios - 不使用分屏调用 Swift viewWillTransitionToSize

ios - 追加字节数组

ios - 无法在按钮上加载 UIImage

ios - 更改 UIImage 基色,保持白色

ios - 如何将 UIButton 带到 Mapbox View 的前面

ios - 在 UIView 上绘制点和线

objective-c - 从 cocos2d 中的点数组绘制线的最有效方法

ios - 防止过于频繁地调用委托(delegate)方法

ios - 如何在 Swift 中以编程方式从另一个图像中剪切 Logo (png 图像)的形状?