各种动画集合

经常用到各种各样的动画,所以,自己写一个简单的类别,封装一些简单的动画操作。前段时间学习了一些关联的用法。本着学以致用的原则(怕不用几天又忘了),所以在封装这个小的类别时,用关联给类别增添了几个新的属性。github地址:https://github.com/n1sunjianfei/Animation


下面是一些简单的用法,当然,可以根据不同的起始点进行其他的动画。

初始化一个view

UIView *testView = [[UIView alloc]init];
    testView.frame = CGRectMake(0, 0, 280, 180);
    testView.backgroundColor = [UIColor grayColor];
    testView.layer.masksToBounds = YES;
    testView.layer.cornerRadius = 5;
    testView.animationTime = 0.5;
    
    
    __weak typeof(self) weakself=self;
    
    testView.finishedIn = ^(void){
        NSLog(@"移入");
        weakself.isShow = YES;
    };
    testView.finishedOut = ^(void){
        NSLog(@"移除");
        weakself.isShow = NO;
    };
    self.testView = testView;

添加几个按钮用以展示动画效果:

- (IBAction)ScaleIn:(UIButton *)sender {
    
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    self.testView.center = CGPointMake(width/2, height/2);
    
    if (!_isShow) {
        [self.testView showInView_Scale:self.view];
        
    }else{
        
        [self.testView removeFromSuperView_Scale];
    }
}
- (IBAction)animationTest:(UIButton *)sender {
    
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    
    if (!_isShow) {
        //贝赛尔曲线滑入
//        [self.testView showInView_curve:self.view startCenter:CGPointMake(0, 0) endCenter:CGPointMake(width/2, height/2)];
        
        //直线滑入
        [self.testView showInView_curve:self.view startCenter:CGPointMake(width/2, 0) endCenter:CGPointMake(width/2, height/2)];
        
    }else{
        
//       [self.testView removeFromSuperView_curveWithStartCenter:self.testView.center endCenter:CGPointMake(width, height)];
        [self.testView removeFromSuperView_curveWithStartCenter:self.testView.center endCenter:CGPointMake(width/2, height)];
    }
}

- (IBAction)Clickin:(UIButton *)sender {
   
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    
    if (!_isShow) {
        [self.testView showInView_Moved:self.view startCenter:CGPointMake(0,0) endCenter:CGPointMake(width/2, height/2)];
        
    }else{
        
        [self.testView removeFromSuperView_MovedWithEndCenter:CGPointMake(width, height)];
    }
    
}
- (IBAction)CATransitionTest:(UIButton *)sender {
    
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    self.testView.center = CGPointMake(width/2, height/2);

    if (!_isShow) {
        [self.testView showInview_Animation:self.view type:kCATransitionMoveIn subType:nil];
        
    }else{
        
        [self.testView removeFromSuperView_AnimationWithType:@"rippleEffect" subType:nil];
        
    }
}
- (IBAction)shake:(UIButton *)sender {
    
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat height = CGRectGetHeight(self.view.frame);
    self.testView.center = CGPointMake(width/2, height/2);

    [self.view addSubview:self.testView];
    [self.testView shake];
}

自己封装的类别中添加了三个属性,关键性代码为:

//声明
@property(nonatomic,assign) CGFloat animationTime;
@property(nonatomic,strong) CompletionBlock finishedIn;
@property(nonatomic,strong) CompletionBlock finishedOut;

使用关联,写三个属性的get和set方法:

#pragma mark - 添加animationTime,两个block属性

-(void)setAnimationTime:(CGFloat)animationTime{
    return objc_setAssociatedObject(self, @selector(animationTime),[NSNumber numberWithFloat:animationTime], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}

-(CGFloat)animationTime{
    
    NSNumber *timeValue = objc_getAssociatedObject(self, _cmd);
    return timeValue.floatValue==0?0.3:timeValue.floatValue;
}

-(void)setFinishedIn:(CompletionBlock)finishedIn{
    return objc_setAssociatedObject(self, @selector(finishedIn), finishedIn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(CompletionBlock)finishedIn{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setFinishedOut:(CompletionBlock)finishedOut{
    return objc_setAssociatedObject(self, @selector(finishedOut), finishedOut, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(CompletionBlock)finishedOut{
    return objc_getAssociatedObject(self, _cmd);
}

Demo中只有部分动画效果,为了方便使用,在后续编码时遇到其他常用动画效果会慢慢填充进去。

原文地址:https://www.cnblogs.com/sunjianfei/p/7086629.html