当遇到严格要求的UI-实现

说明

因为公司项目负责,所以这个代码将用Demo的形式建立,然后引入到项目中.然后对项目的调用进行重构.

创建项目文件

文件清单:

  • 产品父类 GUILayout 包含各种属性
  • iPhone4s/4 布局类 GUIIP4SLayout 继承 GUILayout
  • iPhone5s/5 布局类 GUIIP5Layout 继承 GUILayout
  • iPhone6 布局类 GUIIP6Layout 继承 GUILayout
  • iPhone6 plus 布局类 GUIIP6PLayout 继承 GUILayout
  • 工厂类 GUILayoutFactory

代码详解

首先是 GUILayout,它定义了所有需要的属性.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#import <UIKit/UIKit.h>

@interface GUILayout : NSObject

@property (assign, nonatomic) CGFloat textFieldHeight;
@property (assign, nonatomic) CGFloat textFieldWidth;
@property (assign, nonatomic) CGFloat loginButtonWidth;
@property (assign, nonatomic) CGFloat loginButtonHeight;

@end

#import "GUILayout.h"

@implementation GUILayout

@end

它的布局子类,在 init方法中,对布局属性进行了初始化,因为代码几乎一样,所以拿一个类说明:(为了以后区分,我给不同的布局类不同的值:
4/4s 值都是 4 ,5/5s 值都是5 ,6 值都是6 ,6 plus值都是 60)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import "GUILayout.h"

@interface GUIIP4SLayout : GUILayout

@end

#import "GUIIP4SLayout.h"

@implementation GUIIP4SLayout

- (instancetype)init {
if (self = [super init]) {

self.textFieldHeight = 4;
self.textFieldWidth = 4;
self.loginButtonHeight = 4;
self.loginButtonWidth = 4;

}
return self;
}

@end

然后是工厂类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#import <Foundation/Foundation.h>
@class GUILayout;
@interface GUILayoutFactory : NSObject

-(GUILayout*)createLayout;
+(GUILayout*)createLayout;


@end


#import "GUILayoutFactory.h"
#import "GUIIP4SLayout.h"
#import "GUIIP6Layout.h"
#import "GUIIP6PLayout.h"
#import "GUIIP5Layout.h"

#define kisiPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

#define kisiPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

#define kisiPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)

#define kisiPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242,2208), [[UIScreen mainScreen] currentMode].size) : NO)


typedef NS_ENUM(NSUInteger, GUIDeviceType) {

GUIDeviceType4S = 1,
GUIDeviceType5,
GUIDeviceType6,
GUIDeviceType6P,
GUIDeviceTypeNotFound

};

@implementation GUILayoutFactory

-(GUILayout*)createLayout{

switch ([self deviceType]) {
case GUIDeviceType4S:
return [GUIIP4SLayout new];
break;
case GUIDeviceType6:
return [GUIIP6Layout new];
break;
case GUIDeviceType6P:
return [GUIIP6PLayout new];
break;
case GUIDeviceType5:
return [GUIIP5Layout new];
case GUIDeviceTypeNotFound:
return nil;

}

return nil;


}

+(GUILayout*)createLayout{

return [[GUILayoutFactory new] createLayout];

}

-(GUIDeviceType)deviceType{

if (kisiPhone4) {
return GUIDeviceType4S;
}else if(kisiPhone5){
return GUIDeviceType5;
}else if(kisiPhone6){
return GUIDeviceType6;
}else if(kisiPhone6Plus){
return GUIDeviceType6P;
}else{
return GUIDeviceTypeNotFound;
}

}
@end
  • 将判断逻辑下放到工厂类中,简化了控制器的调用
  • 定义根据屏幕尺寸判断设备类型的宏
  • 根据枚举类型决定返回的layout类型

结语

好了,这就是完整的代码了.根据自己的需求,修改参数名,可以方便的用到自己的系统中.