forked from jackywang135/JW3DDiceView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJW3DDiceView.swift
406 lines (385 loc) · 13.7 KB
/
JW3DDiceView.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
// JW3DDiceView.swift
// JW3DDiceView
//
// Created by Jacky Wang on 5/1/15.
// Copyright (c) 2015 Jacky Wang. All rights reserved.
//
import UIKit
import AVFoundation
import CoreMotion
protocol JW3DDiceViewDelegate {
func diceViewDidReachMaxDiceCount(diceView: JW3DDiceView)
func diceViewDidGoUnderMaxDiceCount(diceView: JW3DDiceView)
func diceViewDidStartRolling(diceView:JW3DDiceView)
func diceViewDidEndRolling(diceView:JW3DDiceView)
func diceViewDidAddDice(diceView:JW3DDiceView)
func diceViewDidDeleteDice(diceView:JW3DDiceView)
}
class JW3DDiceView: UIView, JW3DDiceDelegate {
//Customizable properties
var diceWidth: CGFloat = 60
var diceCount: Int {
get {
return diceViews.count
}
}
var maxDiceCount: Int
var total: Int {
get {
return diceViews.reduce(0){$0 + $1.number}
}
}
var shakeEnabled = true
var deviceMotionEnabled = true {
didSet {
resetMotionManager()
}
}
var soundEnabled = true
var delegate: JW3DDiceViewDelegate?
//Private
private let defaultBackgroundImage = UIImage(named: "defaultBackground.jpg")
private let diceMaxDensityInFrame : CGFloat = 1/7
private let motionManager = CMMotionManager()
private var animator : UIDynamicAnimator?
private let diceBehavior = JWDiceDynamicBehavior()
private var diceViews : [JW3DDice] {
get {
return subviews.filter() {$0 is JW3DDice} as! [JW3DDice]
}
}
private var deviceMotionCalibration: CGFloat {
get {
return UIDevice.currentDevice().userInterfaceIdiom == .Pad ? 150 : 100
}
}
private let audioPlayer = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("ShakeAndRollDice", ofType: "mp3")!), error: nil)
override init(frame: CGRect) {
maxDiceCount = 5
super.init(frame: frame)
initialSetUp()
}
required init(coder aDecoder: NSCoder) {
maxDiceCount = 5
super.init(coder: aDecoder)
initialSetUp()
}
func initialSetUp() {
setMaxCount()
animator = UIDynamicAnimator(referenceView: self)
animator?.addBehavior(diceBehavior)
resetMotionManager()
setBackgroundImage(defaultBackgroundImage!)
}
func setMaxCount() {
maxDiceCount = Int(floor(diceMaxDensityInFrame * ((frame.height * frame.width) / (diceWidth * diceWidth))))
}
func setBackgroundImage(image: UIImage) {
var backgroundImageView = UIImageView(frame: UIScreen.mainScreen().bounds)
backgroundImageView.image = image
backgroundImageView.contentMode = .ScaleAspectFill
addSubview(backgroundImageView)
}
}
//MARK: Dice Operations
extension JW3DDiceView {
func addDice() {
if diceCount == maxDiceCount {
return
}
let diceViewXposition = Int(arc4random_uniform(UInt32(self.frame.width - diceWidth)))
let dice = JW3DDice(frame: CGRectMake(CGFloat(diceViewXposition), 0, diceWidth, diceWidth))
addSubview(dice)
diceBehavior.addItem(dice)
diceBehavior.delegate = dice
dice.delegate = self
playSound()
delegate?.diceViewDidAddDice(self)
delay(0.5) { self.diceBehavior.gravityBehavior.removeItem(dice)}
if diceCount == maxDiceCount {
delegate?.diceViewDidReachMaxDiceCount(self)
return
}
}
func deleteDice(dice: JW3DDice?) {
if let dice = dice {
dice.removeFromSuperview()
} else {
diceViews.last?.removeFromSuperview()
}
delegate?.diceViewDidDeleteDice(self)
if diceCount == maxDiceCount - 1 {
delegate?.diceViewDidGoUnderMaxDiceCount(self)
}
}
func roll() {
delegate?.diceViewDidStartRolling(self)
pushDice()
diceViews.map() { $0.roll()}
}
private func pushDice() {
func getRandomRadians() -> CGFloat {
return CGFloat(arc4random_uniform(UInt32(2 * M_PI)))
}
func getRandomOffset() -> UIOffset {
let randomHorizontalOffset = CGFloat(arc4random_uniform(UInt32(diceWidth/2) - UInt32(diceWidth/4)))
let randomVerticalOffset = CGFloat(arc4random_uniform(UInt32(diceWidth/2) - UInt32(diceWidth/4)))
return UIOffsetMake(randomHorizontalOffset, randomVerticalOffset)
}
for dice in diceViews {
var dicePushBehavior = UIPushBehavior(items:[dice], mode: UIPushBehaviorMode.Instantaneous)
dicePushBehavior.magnitude = 5
dicePushBehavior.angle = getRandomRadians()
dicePushBehavior.active = true
dicePushBehavior.addItem(dice)
dicePushBehavior.setTargetOffsetFromCenter(getRandomOffset(), forItem: dice)
animator?.addBehavior(dicePushBehavior)
delay(1) { animator?.removeBehavior(dicePushBehavior) }
}
}
}
//MARK: JW3DiceDelegate
extension JW3DDiceView {
func diceDidStartRolling(dice: JW3DDice) {
}
func diceDidEndRolling(dice: JW3DDice) {
let activeDice = diceViews.filter(){$0.isAnimating()}
if activeDice.count == 0 {
self.delegate?.diceViewDidEndRolling(self)
}
}
func diceDidReceiveTap(dice: JW3DDice) {
if diceCount == 1 {
return
}
deleteDice(dice)
}
}
//MARK: Sound
extension JW3DDiceView {
private func playSound() {
if !soundEnabled {
return
}
audioPlayer.play()
}
}
//MARK: Shake Motion
extension JW3DDiceView {
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) {
if !shakeEnabled {
return
}
if motion == UIEventSubtype.MotionShake {
roll()
}
}
}
//MARK: Device Motion
extension JW3DDiceView {
private func resetMotionManager() {
if !deviceMotionEnabled {
motionManager.stopDeviceMotionUpdates()
return
}
var deviceMotionHandler : CMDeviceMotionHandler = {data, error in
let rotationX = CGFloat(data.rotationRate.x)
let rotationY = CGFloat(data.rotationRate.y)
let rotationZ = CGFloat(data.rotationRate.z)
let accelerateX = CGFloat(data.userAcceleration.x)
let accelerateY = CGFloat(data.userAcceleration.y)
let accelerateLimit = CGFloat(2)
let rotationLimit = CGFloat(6)
if accelerateX > accelerateLimit || accelerateY > accelerateLimit || rotationX > rotationLimit || rotationY > rotationLimit || rotationZ > rotationLimit {
self.roll()
} else {
self.moveDice(rotationX, rotationY: rotationY, rotationZ: rotationZ, accelerateX: accelerateX, accelerateY: accelerateY)
}
}
motionManager.deviceMotionUpdateInterval = 0.1
motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: deviceMotionHandler)
}
private func moveDice(rotationX : CGFloat, rotationY : CGFloat, rotationZ : CGFloat, accelerateX : CGFloat, accelerateY : CGFloat) {
for dice in diceViews {
diceBehavior.dynamicItemBehavior.addLinearVelocity(CGPointMake(rotationY * deviceMotionCalibration, rotationX * deviceMotionCalibration), forItem: dice)
diceBehavior.dynamicItemBehavior.addAngularVelocity(-rotationZ, forItem: dice)
diceBehavior.dynamicItemBehavior.addLinearVelocity(CGPointMake(accelerateX * deviceMotionCalibration, accelerateY * deviceMotionCalibration), forItem: dice)
}
}
}
protocol JW3DDiceDelegate {
func diceDidStartRolling(dice: JW3DDice)
func diceDidEndRolling(dice: JW3DDice)
func diceDidReceiveTap(dice: JW3DDice)
}
internal class JW3DDice: UIImageView, JWDiceDynamicBehaviorDelegate {
var number: Int{
didSet {
image = JWDiceImageHelper.sharedHelper.getDiceImage(number)
}
}
private var insetValue : CGFloat
var isRolling = false
var didContact = false
var delegate: JW3DDiceDelegate?
var diceWidth: CGFloat {
get {
return self.frame.width
}
}
override init(frame: CGRect) {
insetValue = frame.width / 5
number = 1
super.init(frame: frame)
image = JWDiceImageHelper.sharedHelper.getDiceImage(number)
var tapGesture = UITapGestureRecognizer(target: self, action:"didTap:")
addGestureRecognizer(tapGesture)
userInteractionEnabled = true
}
required init(coder aDecoder: NSCoder) {
insetValue = 0
number = 1
super.init(coder: aDecoder)
insetValue = diceWidth / 5
image = JWDiceImageHelper.sharedHelper.getDiceImage(number)
var tapGesture = UITapGestureRecognizer(target: self, action:"didTap:")
addGestureRecognizer(tapGesture)
userInteractionEnabled = true
}
//Expand & shrink frame before & after animation because static image size is larger than animation images.
func roll() {
if isAnimating() {
return
}
didContact = false
animateWithCompletion({
self.animateRoll()
self.expandSizeWithInsetValue(self.insetValue)
self.delegate?.diceDidStartRolling(self)
}) {
self.shrinkSizeWithInsetValue(self.insetValue)
self.setRandomNumber()
self.delegate?.diceDidEndRolling(self)
}
delay(1){
self.stopAnimating()
}
}
private func animateRoll() {
animationImages = JWDiceImageHelper.sharedHelper.diceAnimateImage
animationRepeatCount = 0
startAnimating()
}
private func setRandomNumber() {
number = Int(arc4random() % 6) + 1
}
private func shrinkSizeWithInsetValue(float : CGFloat) {
self.bounds = CGRectInset(self.bounds, insetValue, insetValue)
}
private func expandSizeWithInsetValue(float: CGFloat) {
self.bounds = CGRectInset(self.bounds, -insetValue, -insetValue)
}
func didTap(gesture:UITapGestureRecognizer) {
delegate?.diceDidReceiveTap(self)
}
//MARK: JWDiceDynamicBehaviorDelegate
func didCollide(dice:JW3DDice) {
if !isAnimating() || didContact {
return
}
dice.didContact = true
delay(0.5){
dice.stopAnimating()
}
}
}
protocol JWDiceDynamicBehaviorDelegate {
func didCollide(dice:JW3DDice)
}
internal class JWDiceDynamicBehavior: UIDynamicBehavior, UICollisionBehaviorDelegate {
var collisionBehavior = UICollisionBehavior()
var dynamicItemBehavior = UIDynamicItemBehavior()
var gravityBehavior = UIGravityBehavior()
var delegate: JWDiceDynamicBehaviorDelegate?
override init() {
super.init()
setUpBehaviors()
addChildBehavior(collisionBehavior)
addChildBehavior(dynamicItemBehavior)
addChildBehavior(gravityBehavior)
}
func setUpBehaviors() {
collisionBehavior.translatesReferenceBoundsIntoBoundary = true
collisionBehavior.collisionDelegate = self
dynamicItemBehavior.elasticity = 0.45
dynamicItemBehavior.resistance = 1
dynamicItemBehavior.angularResistance = 1
gravityBehavior.magnitude = 9
}
func addItem(item : UIDynamicItem) {
collisionBehavior.addItem(item)
gravityBehavior.addItem(item)
dynamicItemBehavior.addItem(item)
}
func removeItem(item : UIDynamicItem) {
collisionBehavior.removeItem(item)
gravityBehavior.removeItem(item)
dynamicItemBehavior.removeItem(item)
}
func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item1: UIDynamicItem, withItem item2: UIDynamicItem, atPoint p: CGPoint) {
if let dice = item1 as? JW3DDice {
delegate?.didCollide(dice)
}
if let dice = item2 as? JW3DDice {
delegate?.didCollide(dice)
}
}
func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying, atPoint p: CGPoint) {
if let dice = item as? JW3DDice {
gravityBehavior.removeItem(dice)
delegate?.didCollide(dice)
}
}
}
internal class JWDiceImageHelper {
//Getting the images from resources can be expensive when many dice are in the field. Therefore, only init the images once and allow access by all dice.
var diceAnimateImage = [UIImage]()
private var diceImage = [UIImage]()
class var sharedHelper :JWDiceImageHelper {
struct singleton {
static let instance = JWDiceImageHelper()
}
return singleton.instance
}
init() {
setUpDiceAnimateImage()
setUpDiceImage()
}
private func setUpDiceAnimateImage() {
var diceImageArray = [UIImage]()
for index in 1...13 {
diceImageArray.append(UIImage(named: "dice\(index)")!)
}
diceAnimateImage = diceImageArray
}
private func setUpDiceImage() {
var diceImageArray = [UIImage]()
for index in 1...6 {
diceImageArray.append(UIImage(named: "\(index)")!)
}
diceImage = diceImageArray
}
func getDiceImage(num : Int) -> UIImage {
return diceImage[(num - 1)] as UIImage
}
}
func delay(delay: Double, closure: () -> ()) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
}
func animateWithCompletion(animation:()->(), completion:()->()) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
animation()
CATransaction.commit()
}