上下抖动的进度条

#import <UIKit/UIKit.h>

@interface TJShakeProgressView : UIView


@property(nonatomic,assign)CGFloat progress;


@end

//

//  TJShakeProgressView.m

//  TJShakeProgressView

//

//  Created by MJ on 15/7/6.

//  Copyright (c) 2015 TJ. All rights reserved.

//

 

#import "TJShakeProgressView.h"

 

@interface TJShakeProgressView ()

{

    UIView *firstView;

    UIView *secondView;

    UIView *thirldView;

    UIView *fourView;

    UIView *contentView;

    NSMutableArray *viewArray;

    NSInteger  count;

    CGFloat temp;

}

@end

 

@implementation TJShakeProgressView

 

- (void)awakeFromNib

{

    [super awakeFromNib];

    

    [self setUp];

    

}

- (void)setUp

{

    contentView = [[UIView alloc]init];

    contentView.translatesAutoresizingMaskIntoConstraints = NO;

    contentView.backgroundColor = [UIColor clearColor];

    firstView = [[UIView alloc]init];

    secondView = [[UIView alloc]init];

    thirldView = [[UIView alloc]init];

    fourView = [[UIView alloc]init];

    [self addSubview:contentView];

    NSArray *contentView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentView)];

     NSArray *contentView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentView)];

    [self addConstraints:contentView_H];

    [self addConstraints:contentView_V];

  

    

   viewArray = [NSMutableArray arrayWithArray:@[firstView,secondView,thirldView,fourView]];

    

    for (UIView *views in viewArray)

    {

      

        views.backgroundColor  = [UIColor colorWithRed:arc4random()%255/256.f green:arc4random()%255/256.f blue:arc4random()%255/256.f alpha:1.f];

        views.translatesAutoresizingMaskIntoConstraints = NO;

        [contentView addSubview:views];

        

        NSLayoutConstraint *constraint_Bottom = [NSLayoutConstraint constraintWithItem:views attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeBottom multiplier:1.f constant:0.f];

        [contentView addConstraint:constraint_Bottom];

    }

    

 

    NSArray *allView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[firstView]-[secondView(firstView)]-[thirldView(secondView)]-[fourView(thirldView)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(firstView,secondView,thirldView,fourView)];

    [contentView addConstraints:allView_H];

    

 

}

- (void)setProgress:(CGFloat )progress

{

    

    if (progress<0.f) {

        _progress = 0.0f;

    }

    else if (progress<=1.0f)

    {

        _progress = progress;

    }

    else

    {

      _progress = 1.f;

    }

    for (UIView *views in viewArray)

    {

        count++;

        temp = count%2?0.75:0.45;

        [views needsUpdateConstraints];

        NSLayoutConstraint *constraint_Height = [NSLayoutConstraint constraintWithItem:views attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeHeight multiplier:(_progress*temp) constant:0.f];

        [contentView addConstraint:constraint_Height];

         [views layoutIfNeeded];

        CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.y"];

        basic.toValue = @(_progress*contentView.bounds.size.height);

        basic.repeatDuration = MAXFLOAT;

        basic.duration = 0.60f;

        basic.autoreverses= YES;

        [views.layer addAnimation:basic forKey:@"test"];

      

       

    }

    

}

 

@end

#import "ViewController.h"
#import "TJShakeProgressView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet TJShakeProgressView *progressView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
     _progressView.progress = 0.81;

}

posted @ 2015-07-07 21:17  曹县三胖暴打大猩猩  阅读(257)  评论(0编辑  收藏  举报