CABasicAnimation - 上下滑动动画

#import <UIKit/UIKit.h>

@interface TJProgressView : UIView
@property(nonatomic,assign)CGFloat progressValue;
- (void)showInView:(UIView *)baseView;
@end
//
//  TJProgressView.m
//  CurveProgressDemo
//
//  Created by MJ on 15/6/12.
//  Copyright (c) 2015年 SNWFMJ. All rights reserved.
//

#import "TJProgressView.h"
#define kContainerViewSize CGSizeMake(30.f, 25.f)
#define kPadding 5.f
@interface TJProgressView ()
{
    UIView *leftView;
    UIView *middleView;
    UIView *rightView;
    UIView *containerView;

}
@end
@implementation TJProgressView
- (instancetype)init
{
    if (self = [super init])
    {
        [self setUp];
    }
    return self;
  }
- (void)setUp
{
    containerView = [[UIView alloc ]init];
    containerView.translatesAutoresizingMaskIntoConstraints = NO;
    containerView.backgroundColor = [UIColor clearColor];
    [self addSubview:containerView];
    NSArray *containerView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containerView)];
     NSArray *containerView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containerView)];
    [self addConstraints:containerView_H];
    [self addConstraints:containerView_V];
    
    
    [self test];

    
    
}
- (void)test
{
    leftView = [[UIView alloc]init];
    leftView.translatesAutoresizingMaskIntoConstraints = NO;
    leftView.backgroundColor = [UIColor yellowColor];
    [containerView addSubview:leftView];
    NSArray *leftView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[leftView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(leftView)];
    [containerView addConstraints:leftView_V];
    
    
    
    middleView = [[UIView alloc]init];
    middleView.translatesAutoresizingMaskIntoConstraints = NO;
    middleView.backgroundColor = [UIColor redColor];
    [containerView addSubview:middleView];
    NSArray *middleView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[middleView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(middleView)];
    [containerView addConstraints:middleView_V];
    
    
    rightView = [[UIView alloc]init];
    rightView.translatesAutoresizingMaskIntoConstraints = NO;
    rightView.backgroundColor = [UIColor blueColor];
    [containerView addSubview:rightView];
    NSArray *rightView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[rightView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(rightView)];
    [containerView addConstraints:rightView_V];
    NSDictionary *dic_paddingConstraint = @{
                                            @"padding":@(kPadding)
                                            
                                            };
    NSArray *allView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[leftView]-padding-[middleView(leftView)]-padding-[rightView(middleView)]-0-|" options:0 metrics:dic_paddingConstraint views:NSDictionaryOfVariableBindings(leftView,middleView,rightView)];
    [containerView addConstraints:allView_H];
    

}
- (void)showInView:(UIView *)baseView
{
    NSDictionary *dic_selfConstraint = @{
                                     @"height":@(kContainerViewSize.height),
                                     @"width":@(kContainerViewSize.width)
                                     
                                     };
    self.translatesAutoresizingMaskIntoConstraints = NO;
    [baseView addSubview:self];
    
    NSLayoutConstraint *self_CenterX = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:baseView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *self_CenterY = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:baseView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    
    NSArray *self_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[self(width)]" options:0 metrics:dic_selfConstraint views:NSDictionaryOfVariableBindings(self)];
    NSArray *self_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[self(height)]" options:0 metrics:dic_selfConstraint views:NSDictionaryOfVariableBindings(self)];
    
    [baseView addConstraints:@[self_CenterX,self_CenterY]];
    [baseView addConstraints:self_H];
    [baseView addConstraints:self_V];
    
    
}
- (void)setProgressValue:(CGFloat)progressValue
{
    _progressValue = progressValue;
    
    [self addAnimation:containerView.layer progressValue:_progressValue];
   
}
- (void)addAnimation:(CALayer *)layer progressValue:(CGFloat)value
{
    CABasicAnimation *leftAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    leftAnimation.fromValue =@0;
    leftAnimation.toValue = @(value*kContainerViewSize.height);
    leftAnimation.repeatCount = MAXFLOAT;
    leftAnimation.duration = 0.5f;
    
    [layer addAnimation:leftAnimation forKey:@"test"];
}

@end


//
//  ViewController.m
//  CurveProgressDemo
//
//  Created by MJ on 15/6/12.
//  Copyright (c) 2015年 SNWFMJ. All rights reserved.
//

#import "ViewController.h"
#import "TJProgressView.h"
@interface ViewController ()
{
    TJProgressView *progressView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    progressView = [[TJProgressView alloc]init];
    [progressView showInView:self.view];
   
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeValue) userInfo:nil repeats:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
- (void)changeValue
{
    progressView.progressValue =0.5;
}
@end

转载于:https://www.cnblogs.com/thbbsky/p/4572068.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值