ProgressView

接口部分

@protocol ProgressViewDelegate <NSObject>

-(void)progressViewProgressComplete;

@end

typedef NS_ENUM(NSInteger, ProgressDirectionType) {
    ProgressDirectionType_vertical,
    ProgressDirectionType_horizontal,
};


@interface ProgressView : UIView

@property (nonatomic,assign) CGPoint beginPoint;
@property (nonatomic,assign) CGFloat progressWidth;
@property (nonatomic,assign) CGFloat limitLength;
@property (nonatomic,assign) ProgressDirectionType directon;

@property (nonatomic,assign) CGFloat progress;//0~1

@property (nonatomic, weak) id<ProgressViewDelegate> delegate;

- (instancetype)initWithBeginPoint:(CGPoint)beginPoint progressWidth:(CGFloat)progressWidth limitLength:(CGFloat)limitLength directon:(ProgressDirectionType)directon;
@end

实现部分

@interface ProgressView ()

@end

@implementation ProgressView


- (void)setBeginPoint:(CGPoint)beginPoint
{
    _beginPoint = beginPoint;
    self.frame = CGRectMake(beginPoint.x, beginPoint.y, self.frame.size.width, self.frame.size.height);
}
-(void)setLimitLength:(CGFloat)limitLength
{
    _limitLength = limitLength;
}
- (void)setDirecton:(ProgressDirectionType)directon
{
    _directon = directon;
    self.progressWidth = self.progressWidth;
}
-(void)setProgressWidth:(CGFloat)progressWidth
{
    _progressWidth = progressWidth;
    if (self.directon == ProgressDirectionType_horizontal) {
        self.frame = CGRectMake(self.beginPoint.x, self.beginPoint.y, 0, progressWidth);
    }
    else if (self.directon == ProgressDirectionType_vertical)
    {
        self.frame = CGRectMake(self.beginPoint.x, self.beginPoint.y, progressWidth, 0);
    }
}
-(void)setProgress:(CGFloat)progress
{
    _progress = progress;
    CGFloat currentLength = progress * self.limitLength;
    
    if (self.directon == ProgressDirectionType_horizontal) {
        if (progress == 0) {
            self.frame = CGRectMake(self.beginPoint.x, self.beginPoint.y, currentLength, self.progressWidth);
        }
        else
        {
            [UIView animateWithDuration:0.2 animations:^{
                self.frame = CGRectMake(self.beginPoint.x, self.beginPoint.y, currentLength, self.progressWidth);
            } completion:^(BOOL finished) {
                if (progress >= 1) {
                    self.progress = 0;
                    if (self.delegate) {
                        if ([self.delegate respondsToSelector:@selector(progressViewProgressComplete)]) {
                            [self.delegate progressViewProgressComplete];
                        }
                    }
                }
            }];
        }
    }
    else if (self.directon == ProgressDirectionType_vertical)
    {
        if (progress == 0) {
            self.frame = CGRectMake(self.beginPoint.x, self.beginPoint.y, self.progressWidth, currentLength);
        }
        else
        {
            [UIView animateWithDuration:0.2 animations:^{
                self.frame = CGRectMake(self.beginPoint.x, self.beginPoint.y, self.progressWidth, currentLength);
            } completion:^(BOOL finished) {
                self.progress = 0;
            }];
        }
    }
    else
    {}
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.beginPoint = CGPointMake(0, 0);
        self.directon = ProgressDirectionType_horizontal;
        self.progressWidth = 1;
        self.limitLength = UIScreen.mainScreen.bounds.size.width;
    }
    return self;
}
- (instancetype)initWithBeginPoint:(CGPoint)beginPoint progressWidth:(CGFloat)progressWidth limitLength:(CGFloat)limitLength directon:(ProgressDirectionType)directon
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        self.beginPoint = beginPoint;
        self.limitLength = limitLength;
        self.directon = directon;
        self.progressWidth = progressWidth;
    }
    
    return self;
}

@end

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

相关阅读更多精彩内容

友情链接更多精彩内容