Permalink
Browse files

Added gesture recognisers

  • Loading branch information...
jrturton committed Jul 12, 2016
1 parent 66f0b6d commit 72b24c33a1824b6de23bf5bbee9e73d321a721d4
Showing with 81 additions and 1 deletion.
  1. +21 −0 Pentominoes/Base.lproj/Main.storyboard
  2. +60 −1 Pentominoes/PentominoesViewController.swift
@@ -17,9 +17,30 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<gestureRecognizers/>
<connections>
<outletCollection property="gestureRecognizers" destination="zzi-L9-7Va" appends="YES" id="5Rz-MP-lCt"/>
<outletCollection property="gestureRecognizers" destination="5gy-Pc-Dai" appends="YES" id="9ve-IB-loS"/>
</connections>
</view>
<connections>
<outlet property="pan" destination="5gy-Pc-Dai" id="5XS-9t-SRU"/>
<outlet property="tap" destination="zzi-L9-7Va" id="tQd-Vs-0Vu"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
<tapGestureRecognizer id="zzi-L9-7Va">
<connections>
<action selector="handleTap:" destination="BYZ-38-t0r" id="LdE-hu-pgC"/>
<outlet property="delegate" destination="BYZ-38-t0r" id="FoT-mt-wOf"/>
</connections>
</tapGestureRecognizer>
<panGestureRecognizer minimumNumberOfTouches="1" maximumNumberOfTouches="1" id="5gy-Pc-Dai">
<connections>
<action selector="handlePan:" destination="BYZ-38-t0r" id="4ru-Ld-i89"/>
<outlet property="delegate" destination="BYZ-38-t0r" id="EXE-kg-iQR"/>
</connections>
</panGestureRecognizer>
</objects>
</scene>
</scenes>
@@ -2,7 +2,10 @@ import UIKit
public class PentominoesViewController: UIViewController {
private let gridSize: CGFloat = 30
@IBOutlet var tap: UITapGestureRecognizer!
@IBOutlet var pan: UIPanGestureRecognizer!
private let gridSize: CGFloat = 35
var boardView: BoardView!
var tileViews: [TileView]!
public var board: Board! {
@@ -48,4 +51,60 @@ public class PentominoesViewController: UIViewController {
}
}
var activeTile: TileView? {
willSet {
if let oldActiveTile = activeTile {
oldActiveTile.lifted = false
oldActiveTile.layer.zPosition = 0
}
}
didSet {
if let newActiveTile = activeTile {
newActiveTile.lifted = true
newActiveTile.layer.zPosition = 10
}
}
}
@IBAction func handleTap(sender: UITapGestureRecognizer) {
activeTile?.rotate(true)
}
@IBAction func handlePan(sender: UIPanGestureRecognizer) {
var fingerClearedLocation = sender.locationInView(view)
fingerClearedLocation.y -= (activeTile?.bounds.height ?? 0.0) * 0.5
switch sender.state {
case .Began:
UIView.animateWithDuration(0.1) { self.activeTile?.center = fingerClearedLocation }
case .Changed:
activeTile?.center = fingerClearedLocation
case .Ended, .Cancelled:
// TODO: put it down in a permissible place
activeTile = nil
default:
break
}
}
}
extension PentominoesViewController: UIGestureRecognizerDelegate {
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
public func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer != pan {
return true
}
let location = gestureRecognizer.locationInView(view)
if let hitTile = view.hitTest(location, withEvent: nil) as? TileView {
activeTile = hitTile
return true
}
return false
}
}

0 comments on commit 72b24c3

Please sign in to comment.