我需要帮助在Objective-c中以编程方式创建此图像。凹半径是图像高度的一半。我的目标是有一个进度条,左侧是图像,中间是……
我可能只是应用一个而不是创建任何图像 cornerRadius 同时显示主要进度视图以及显示进度的视图。因此,想象一下以下视图层次结构:
cornerRadius
主要进展视图是背面的白色边框视图。这有一个子视图, progressSubview (在上面以蓝色突出显示),显示了迄今为止的进展(并且会随着我们更新而改变 progress 属性)。绿色和蓝色视图只是固定大小的子视图 progressSubview 被揭示为 progressSubview 剪辑其子视图,改变大小。
progressSubview
progress
应用角半径非常容易。并通过避免任何图像或自定义 drawRect ,我们可以动画的变化 progressSubview 如果我们想要:
drawRect
例如。
// 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; }];
...我在'UIView *'语义警告类型的对象上找不到属性“进度”
看起来你的 IBOutlet 被定义为a UIView 而不是一个 CustomProgressView 。
IBOutlet
UIView
CustomProgressView