在我心里,面向对象应该是这样的:
“给马一个规则,让它只能走日”设计模式
而MVVM看上去是将方法从VC分离出来,但是并没有这种感觉,所以我就尝试用我想要的方式写了一个象棋棋子“马”,我称这种设计模式为“给马一个规则,让它只能走日”设计模式.
讲之前先说一下WWeChat,其实更新了,只是没写讲解…改动的还是挺多的,代码在这里.
效果图:
Chess.gif
下面讲我这种设计模式(如果你有建议,欢迎指出!)
-
首先我创建了一个
UIButtom
,名字是Chess
顾名思义,这个类是所有棋子的父类
.h
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 |
/** * 棋子上的Label */ @property (nonatomic,strong)UILabel * categoryLabel; /** * 棋子的类别 (红,黑)默认为红 */ @property (nonatomic,assign)BOOL isRed; /** * 棋子的状态 (拿起,放下)默认为放下 */ @property (nonatomic,assign)BOOL isInAir; /** * 每格的宽度 */ @property (nonatomic,assign)CGFloat rowWidth; /** * 目前的行列 */ @property (nonatomic,copy)NSIndexPath * index; - (instancetype)initWithRowWidth:(CGFloat)rowWidth andRowLine:(NSIndexPath *)index; |
.m
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 |
- (instancetype)initWithRowWidth:(CGFloat)rowWidth andRowLine:(NSIndexPath *)index { if (self = [super init]) { _isRed = YES; _isInAir = NO; _index = index; _rowWidth = rowWidth; self.backgroundColor = [UIColor clearColor]; //行 NSInteger row = index.row; //列 NSInteger line = index.section; if (row > 5) { self.frame = CGRectMake(row * rowWidth - rowWidth/2.0 , line * rowWidth - rowWidth/2.0 + rowWidth, rowWidth, rowWidth); } else { self.frame = CGRectMake(row * rowWidth - rowWidth/2.0, line * rowWidth - rowWidth/2.0,rowWidth, rowWidth); } self.categoryLabel = ({ UILabel * categoryLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, rowWidth, rowWidth)]; -h"> rowWidth, rowWidth)]; 棋棋子“马”,我称这种设计模式为“给马一个规则,让它只能走日”设计模式.
讲之前先说一下WWeChat,其实更新了,只是没写讲解…改动的还是挺多的,代码在这里. 效果图: Chess.gif
下面讲我这种设计模式(如果你有建议,欢迎指出!)
.h
.m
|