设计复杂的 iOS 动画效果

设计复杂的动画效果

制定统一的动画接口

动画中的高内聚低耦合原理:

如果一个类承担的职能过多,就等于把这些职能耦合在一起,一个职能的变化可能会削弱或者抑制其他职能的能力,这种耦合会导致脆弱的设计,当发生变化时,设计会遭到意想不到的破坏。如果想要避免这种现象发生,就要尽可能的遵守单一职能原则。


设计动画函数的注意事项

用里氏代换原则来处理动画类的继承问题

动画中的模块化设计


1.如果以下代码写在控制器中,实现动画的细节暴露在外,后续方便组合成复杂的动画效果很困难

- (void)viewDidLoad {

    [superviewDidLoad];

    // 初始化view

    self.lineView                 = [[UIViewalloc]initWithFrame:CGRectMake(10,50,100,3)];

    self.lineView.backgroundColor = [UIColorblackColor];

    self.lineView.alpha           =0.f;

    [self.viewaddSubview:self.lineView];

    

    // 延时2秒执行持续时间为1秒的动画 --> show

    [UIViewanimateWithDuration:1delay:0options:UIViewAnimationOptionLayoutSubviewsanimations:^{

        self.lineView.alpha =1.f;

        self.lineView.frame =CGRectMake(10 +50, 50, 100, 3);

    }completion:^(BOOL finished) {

        

    }];

    

    // 延时6秒执行持续时间为1秒的动画 --> hide

    [UIViewanimateWithDuration:1delay:6options:UIViewAnimationOptionLayoutSubviewsanimations:^{

        self.lineView.alpha =0.f;

        self.lineView.frame =CGRectMake(10 +50 + 50, 100, 100, 3);

    }completion:^(BOOL finished) {

        

    }];

}

2.我们新建一个LineView类, 不要把实现动画的细节暴露在外,设计动画类尽量要符合单一职能原则,以便后续方便组合成复杂的动画效果

@interface LineView :UIView


@property (nonatomic)CGFloat  offsetX;


// 显示动画

- (void)show;


// 隐藏动画

- (void)hide;


@end


#import "LineView.h"


@interface LineView ()

@property (nonatomic)CGRect rect;

@end


@implementation LineView


- (instancetype)initWithFrame:(CGRect)frame {

    self = [superinitWithFrame:frame];

    if (self) {

        self.rect = frame;

    }

    return self;

}


- (void)show {

    

    CGRect newRect =CGRectMake(self.rect.origin.x +self.offsetX,

                               self.rect.origin.y,

                               self.rect.size.width,

                               self.rect.size.height);

    

    [UIViewanimateWithDuration:1animations:^{

        self.frame = newRect;

    }];

}


- (void)hide {

    CGRect newRect =CGRectMake(self.rect.origin.x +self.offsetX +self.offsetX,

                               self.rect.origin.y,

                               self.rect.size.width,

                               self.rect.size.height);

    

    [UIViewanimateWithDuration:1animations:^{

        self.frame = newRect;

        self.alpha =0.f;

    }];

}

2.1 、在控制器中写如下

@property (nonatomic,strong)LineView *lineViewNew;


- (void)viewDidLoad {

    //高内聚低耦合的写法

    self.lineViewNew                 = [[LineViewalloc]initWithFrame:CGRectMake(10,200,100,3)];

    self.lineViewNew.offsetX         =50.f;

    self.lineViewNew.backgroundColor = [UIColorblackColor];

    [self.viewaddSubview:self.lineViewNew];

    [self.lineViewNew show];

    [selfperformSelector:@selector(excuteAfterDelay)withObject:nilafterDelay:6];

}


- (void)excuteAfterDelay {

    [self.lineViewNew hide];

}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值