首页 > 代码库 > 各种动画集合
各种动画集合
经常用到各种各样的动画,所以,自己写一个简单的类别,封装一些简单的动画操作。前段时间学习了一些关联的用法。本着学以致用的原则(怕不用几天又忘了),所以在封装这个小的类别时,用关联给类别增添了几个新的属性。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 =http://www.mamicode.com/ objc_getAssociatedObject(self, _cmd); return timeValue.floatValue=http://www.mamicode.com/=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中只有部分动画效果,为了方便使用,在后续编码时遇到其他常用动画效果会慢慢填充进去。
各种动画集合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。