iOS 砖家纪实录00『 基础知识 』首页投稿(暂停使用,暂停投稿)

设计复杂的iOS动画效果

2016-08-06  本文已影响255人  Chars

动画的世界丰富多彩,令人陶醉

随着工作的深入,其实每个 App 中,最关键的莫过于用户交互,随着技术时代的发展,人们已经不再满足于手指点点碰碰就完成功能。用户更加期待炫酷的操作体验。需要眼前一亮的感觉。这些都是 App 将要解决的问题。而动画交互无疑是最多的一种手段。但是,现在也不是简单动画的天下,需要更多的创意,繁杂而简洁的创意。

动画效果的UIView Object

结构图
// 显示动画
- (void)showWithDuration:(CGFloat)duration animated:(BOOL)animated;

// 隐藏动画
- (void)hideWithDuration:(CGFloat)duration animated:(BOOL)animated;

// 创建view
- (void)buildView;

// 动画百分比(手动设置动画的程度)
- (void)percent:(CGFloat)percent;

制定统一的动画接口

即:相关有动画效果的类,有同一的动画方法命名

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

设计动画函数的注意事项

动画效果就是frame值的重新设定

[UIView animateWithDuration:duration animations:^{
        self.frame = self.midRect;
        self.alpha = 1.f;
    }];

一个具有动画效果的类

//  TranslateView.h
#import <UIKit/UIKit.h>

@interface TranslateView : UIView

//** 显示动画 */
- (void)show;

/** 隐藏动画 */
- (void)hide;

/** 创建view */
- (void)buildView;

/** 动画百分比(手动设置动画的程度) */
- (void)percent:(CGFloat)percent;

@end
//  TranslateView.m
#import "TranslateView.h"

@interface TranslateView ()

@property (nonatomic) CGFloat offsetY;
@property (nonatomic) CGRect startRect;
@property (nonatomic) CGRect midRect;
@property (nonatomic) CGRect endRect;
@property (nonatomic) CGRect curRect;

@end

@implementation TranslateView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0.f;
        self.offsetY = 20.0f;
        self.backgroundColor = [UIColor blackColor];
        [self buildView];
    }
    return self;
}

// 创建view
- (void)buildView {
    self.startRect = self.frame;
    self.midRect   = CGRectMake(self.startRect.origin.x,
                                self.startRect.origin.y + self.offsetY,
                                self.startRect.size.width,
                                self.startRect.size.height);
    self.endRect   = CGRectMake(self.startRect.origin.x,
                                self.startRect.origin.y + self.offsetY * 2,
                                self.startRect.size.width,
                                self.startRect.size.height);
}


// 显示动画
- (void)showWithDuration:(CGFloat)duration animated:(BOOL)animated {
    if (animated == YES) {
        [UIView animateWithDuration:duration animations:^{
            self.frame = self.midRect;
            self.alpha = 1.f;
            self.curRect = self.frame;
        }];
    } else {
        self.frame = self.midRect;
        self.alpha = 1.f;
        self.curRect = self.frame;
    }
}

// 隐藏动画
- (void)hideWithDuration:(CGFloat)duration animated:(BOOL)animated {
    if (animated == YES) {
        [UIView animateWithDuration:duration animations:^{
            self.frame = self.endRect;
            self.alpha = 0.f;
        } completion:^(BOOL finished) {
            self.frame = self.startRect;
        }];
    } else {
        self.frame = self.startRect;
        self.alpha = 0.f;
    }
}



// 动画百分比(手动设置动画的程度)
- (void)percent:(CGFloat)percent {
    CGFloat tmpOffsetY = 0;
    CGFloat stateHeight = 20;
    if (percent <= 0) {
        tmpOffsetY = percent * self.offsetY;
    } else if (percent >= 1) {
        tmpOffsetY = self.offsetY;
    } else {
        tmpOffsetY = percent * self.offsetY;
    }

    if (CGRectGetMinY(self.curRect) + tmpOffsetY < stateHeight || CGRectGetMaxY(self.curRect) + tmpOffsetY > CGRectGetHeight([UIScreen mainScreen].bounds)) {
        return;
    }
    
    self.frame = CGRectMake(self.curRect.origin.x,
                            self.curRect.origin.y + tmpOffsetY,
                            self.curRect.size.width,
                            self.curRect.size.height);
    self.curRect = self.frame;
}

- (void)show
{
    [self showWithDuration:2.0f animated:YES];
}

- (void)hide
{
    [self hideWithDuration:0.5f animated:YES];
}
@end

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

SourceView *tmpView = [[ChildTwoView alloc] init];
[tmpView show];

什么是多态

1.定义

某一类事物的多种表现形态

举例说明:

1)生活中:动物:猫—波斯猫

2)程序中:父类指针指向子类对象

2.条件

动物 a = [猫 alloc]init];
动物 a = [狗 alloc]init];

表现形式:在父类指针指向不同的对象的时候,通过父类指针调用被重写的方法,会执行该指针所指向的那个对象的方法;

3.实现

@interface Computer : NSObject
- (void)system;
@end

@interface PC : Computer
// 重写system方法
@end

@interface Mac : Computer
// 重写system方法
@end

Computer *com = nil;

Computer = [PC alloc]init]; //实例化PC对象
[PC system];

Computer = [Mac alloc]init; //实例化Mac对象
[Mac system];

4.原理

ObjC 不同于其他程序设计语言,它可以在运行的时候加入新的数据类型和新的程序模块,动态类型识别,动态绑定,动态加载。

5.优点:

详细参考

Objective-C 继承和多态

动画中的模块化设计

//ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // 复杂的动画被写进了BaseAnimationView当中,没有暴露不必要的细节
    BaseAnimationView *baseView = [[BaseAnimationView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:baseView];
    [baseView show];
}
//BaseAnimationView.m

- (void)show
{
    [self.translateView show];
}

- (void)hide
{
    [self.translateView hide];
}

- (void)buildView
{
    self.translateView = [[TranslateView alloc] initWithFrame:CGRectMake(50, 100, 2, 50)];
    [self addSubview:self.translateView];
}

- (void)percent:(CGFloat)percent
{
    [self.translateView percent:percent];
}

延时执行某方法

[self performSelector:@selector(excuteAfterDelay) withObject:nil afterDelay:6];

初始化UIView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // ......
    }
    return self;
}

Demo on Github

原文阅读

上一篇 下一篇

猜你喜欢

热点阅读