Skip to content

Commit 72b24c3

Browse files
committed
Added gesture recognisers
1 parent 66f0b6d commit 72b24c3

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

Pentominoes/Base.lproj/Main.storyboard

+21
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,30 @@
1717
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
1818
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1919
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
20+
<gestureRecognizers/>
21+
<connections>
22+
<outletCollection property="gestureRecognizers" destination="zzi-L9-7Va" appends="YES" id="5Rz-MP-lCt"/>
23+
<outletCollection property="gestureRecognizers" destination="5gy-Pc-Dai" appends="YES" id="9ve-IB-loS"/>
24+
</connections>
2025
</view>
26+
<connections>
27+
<outlet property="pan" destination="5gy-Pc-Dai" id="5XS-9t-SRU"/>
28+
<outlet property="tap" destination="zzi-L9-7Va" id="tQd-Vs-0Vu"/>
29+
</connections>
2130
</viewController>
2231
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
32+
<tapGestureRecognizer id="zzi-L9-7Va">
33+
<connections>
34+
<action selector="handleTap:" destination="BYZ-38-t0r" id="LdE-hu-pgC"/>
35+
<outlet property="delegate" destination="BYZ-38-t0r" id="FoT-mt-wOf"/>
36+
</connections>
37+
</tapGestureRecognizer>
38+
<panGestureRecognizer minimumNumberOfTouches="1" maximumNumberOfTouches="1" id="5gy-Pc-Dai">
39+
<connections>
40+
<action selector="handlePan:" destination="BYZ-38-t0r" id="4ru-Ld-i89"/>
41+
<outlet property="delegate" destination="BYZ-38-t0r" id="EXE-kg-iQR"/>
42+
</connections>
43+
</panGestureRecognizer>
2344
</objects>
2445
</scene>
2546
</scenes>

Pentominoes/PentominoesViewController.swift

+60-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import UIKit
22

33
public class PentominoesViewController: UIViewController {
44

5-
private let gridSize: CGFloat = 30
5+
@IBOutlet var tap: UITapGestureRecognizer!
6+
@IBOutlet var pan: UIPanGestureRecognizer!
7+
8+
private let gridSize: CGFloat = 35
69
var boardView: BoardView!
710
var tileViews: [TileView]!
811
public var board: Board! {
@@ -48,4 +51,60 @@ public class PentominoesViewController: UIViewController {
4851

4952
}
5053
}
54+
55+
var activeTile: TileView? {
56+
willSet {
57+
if let oldActiveTile = activeTile {
58+
oldActiveTile.lifted = false
59+
oldActiveTile.layer.zPosition = 0
60+
}
61+
}
62+
didSet {
63+
if let newActiveTile = activeTile {
64+
newActiveTile.lifted = true
65+
newActiveTile.layer.zPosition = 10
66+
}
67+
}
68+
}
69+
70+
@IBAction func handleTap(sender: UITapGestureRecognizer) {
71+
activeTile?.rotate(true)
72+
}
73+
74+
@IBAction func handlePan(sender: UIPanGestureRecognizer) {
75+
var fingerClearedLocation = sender.locationInView(view)
76+
fingerClearedLocation.y -= (activeTile?.bounds.height ?? 0.0) * 0.5
77+
switch sender.state {
78+
case .Began:
79+
UIView.animateWithDuration(0.1) { self.activeTile?.center = fingerClearedLocation }
80+
case .Changed:
81+
activeTile?.center = fingerClearedLocation
82+
case .Ended, .Cancelled:
83+
// TODO: put it down in a permissible place
84+
activeTile = nil
85+
default:
86+
break
87+
}
88+
}
89+
90+
91+
92+
}
93+
94+
extension PentominoesViewController: UIGestureRecognizerDelegate {
95+
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
96+
return true
97+
}
98+
99+
public func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
100+
if gestureRecognizer != pan {
101+
return true
102+
}
103+
let location = gestureRecognizer.locationInView(view)
104+
if let hitTile = view.hitTest(location, withEvent: nil) as? TileView {
105+
activeTile = hitTile
106+
return true
107+
}
108+
return false
109+
}
51110
}

0 commit comments

Comments
 (0)