-
Notifications
You must be signed in to change notification settings - Fork 1
/
GridView.swift
116 lines (109 loc) · 3.73 KB
/
GridView.swift
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// grid.swift
// swift-csj2048
//
// Created by csj on 15/6/30.
// Copyright (c) 2015年 csj. All rights reserved.
//
import UIKit
// MARK: - 扩展UIView属性方便计算
extension UIView {
var width: CGFloat {
return self.frame.size.width
}
var height: CGFloat {
return self.frame.size.height
}
}
// MARK: - 初始化
class GridView: UIView {
var lb: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.layer.cornerRadius = width / 5
self.backgroundColor = UIColor.clearColor()
lb = UILabel()
lb.frame = CGRect(x: 0, y: 0, width: width, height: height)
lb.backgroundColor = UIColor.clearColor()
lb.textAlignment = NSTextAlignment.Center
lb.font = UIFont.systemFontOfSize(30)
self.addSubview(lb)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: - 颜色
extension GridView {
func getBKColor(value: Int) -> UIColor {
switch value {
case 0:
return UIColor(red: 85.0/255.0, green: 85.0/255.0, blue: 85.0/255.0, alpha: 1.0)
case 2:
return UIColor(red: 238.0/255.0, green: 228.0/255.0, blue: 218.0/255.0, alpha: 1.0)
case 4:
return UIColor(red: 237.0/255.0, green: 224.0/255.0, blue: 200.0/255.0, alpha: 1.0)
case 8:
return UIColor(red: 242.0/255.0, green: 177.0/255.0, blue: 121.0/255.0, alpha: 1.0)
case 16:
return UIColor(red: 245.0/255.0, green: 149.0/255.0, blue: 99.0/255.0, alpha: 1.0)
case 32:
return UIColor(red: 246.0/255.0, green: 124.0/255.0, blue: 95.0/255.0, alpha: 1.0)
case 64:
return UIColor(red: 246.0/255.0, green: 94.0/255.0, blue: 59.0/255.0, alpha: 1.0)
case 128, 256, 512, 1024, 2048:
return UIColor(red: 237.0/255.0, green: 207.0/255.0, blue: 114.0/255.0, alpha: 1.0)
default:
return UIColor.whiteColor()
}
}
func getNumColor(value: Int) -> UIColor {
switch value {
case 0:
return UIColor.clearColor()
case 2, 4:
return UIColor(red: 119.0/255.0, green: 110.0/255.0, blue: 101.0/255.0, alpha: 1.0)
default:
return UIColor.whiteColor()
}
}
}
// MARK: - 动画
extension GridView {
/**
移动中心到center位置,然后完成completion动作
- parameter time: 动画时间
- parameter center: 移动中心
*/
func moveToCenter(time: NSTimeInterval, center: CGPoint){
UIView.animateWithDuration(time, animations: { () -> Void in
self.center = center
})
}
/**
显示
- parameter value: 数值
- parameter time: 动画时间
*/
func display(value: Int, time: NSTimeInterval){
self.lb.text = "\(value)"
self.lb.textColor = getNumColor(value)
self.backgroundColor = getBKColor(value)
self.transform = CGAffineTransformMakeScale(0.1, 0.1)
UIView.animateWithDuration(time / 2, animations: { () -> Void in
self.transform = CGAffineTransformMakeScale(1.12, 1.12)
}, completion: { (_) -> Void in
UIView.animateWithDuration(time / 4, animations: { () -> Void in
self.transform = CGAffineTransformMakeScale(0.9, 0.9)
}){
(_) -> Void in
UIView.animateWithDuration(time / 4, animations: { () -> Void in
self.transform = CGAffineTransformMakeScale(1, 1)
})
}
})
}
func disappear(){
self.removeFromSuperview()
}
}