美文网首页
ProgressView

ProgressView

作者: 和女神经常玩 | 来源:发表于2023-01-02 00:00 被阅读0次

接口部分

@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

相关文章

网友评论

      本文标题:ProgressView

      本文链接:https://www.haomeiwen.com/subject/clffcdtx.html