码迷,mamicode.com
首页 > 其他好文 > 详细

CABasicAnimation - 上下滑动动画

时间:2015-06-12 18:52:26      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

#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

技术分享

CABasicAnimation - 上下滑动动画

标签:

原文地址:http://www.cnblogs.com/thbbsky/p/4572068.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!