Skip to content

Commit aebe55a

Browse files
committed
Chapter 12
Chapter 12
1 parent 69ad64d commit aebe55a

File tree

15 files changed

+1012
-0
lines changed

15 files changed

+1012
-0
lines changed

12-TouchEvents/TouchEvents.xcodeproj/project.pbxproj

+423
Large diffs are not rendered by default.

12-TouchEvents/TouchEvents.xcodeproj/project.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0610"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "2DE8D0021A64860B005C8B35"
18+
BuildableName = "TouchEvents.app"
19+
BlueprintName = "TouchEvents"
20+
ReferencedContainer = "container:TouchEvents.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "YES"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "YES">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "2DE8D0171A64860B005C8B35"
32+
BuildableName = "TouchEventsTests.xctest"
33+
BlueprintName = "TouchEventsTests"
34+
ReferencedContainer = "container:TouchEvents.xcodeproj">
35+
</BuildableReference>
36+
</BuildActionEntry>
37+
</BuildActionEntries>
38+
</BuildAction>
39+
<TestAction
40+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
41+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
42+
shouldUseLaunchSchemeArgsEnv = "YES"
43+
buildConfiguration = "Debug">
44+
<Testables>
45+
<TestableReference
46+
skipped = "NO">
47+
<BuildableReference
48+
BuildableIdentifier = "primary"
49+
BlueprintIdentifier = "2DE8D0171A64860B005C8B35"
50+
BuildableName = "TouchEventsTests.xctest"
51+
BlueprintName = "TouchEventsTests"
52+
ReferencedContainer = "container:TouchEvents.xcodeproj">
53+
</BuildableReference>
54+
</TestableReference>
55+
</Testables>
56+
<MacroExpansion>
57+
<BuildableReference
58+
BuildableIdentifier = "primary"
59+
BlueprintIdentifier = "2DE8D0021A64860B005C8B35"
60+
BuildableName = "TouchEvents.app"
61+
BlueprintName = "TouchEvents"
62+
ReferencedContainer = "container:TouchEvents.xcodeproj">
63+
</BuildableReference>
64+
</MacroExpansion>
65+
</TestAction>
66+
<LaunchAction
67+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
68+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
69+
launchStyle = "0"
70+
useCustomWorkingDirectory = "NO"
71+
buildConfiguration = "Debug"
72+
ignoresPersistentStateOnLaunch = "NO"
73+
debugDocumentVersioning = "YES"
74+
allowLocationSimulation = "YES">
75+
<BuildableProductRunnable>
76+
<BuildableReference
77+
BuildableIdentifier = "primary"
78+
BlueprintIdentifier = "2DE8D0021A64860B005C8B35"
79+
BuildableName = "TouchEvents.app"
80+
BlueprintName = "TouchEvents"
81+
ReferencedContainer = "container:TouchEvents.xcodeproj">
82+
</BuildableReference>
83+
</BuildableProductRunnable>
84+
<AdditionalOptions>
85+
</AdditionalOptions>
86+
</LaunchAction>
87+
<ProfileAction
88+
shouldUseLaunchSchemeArgsEnv = "YES"
89+
savedToolIdentifier = ""
90+
useCustomWorkingDirectory = "NO"
91+
buildConfiguration = "Release"
92+
debugDocumentVersioning = "YES">
93+
<BuildableProductRunnable>
94+
<BuildableReference
95+
BuildableIdentifier = "primary"
96+
BlueprintIdentifier = "2DE8D0021A64860B005C8B35"
97+
BuildableName = "TouchEvents.app"
98+
BlueprintName = "TouchEvents"
99+
ReferencedContainer = "container:TouchEvents.xcodeproj">
100+
</BuildableReference>
101+
</BuildableProductRunnable>
102+
</ProfileAction>
103+
<AnalyzeAction
104+
buildConfiguration = "Debug">
105+
</AnalyzeAction>
106+
<ArchiveAction
107+
buildConfiguration = "Release"
108+
revealArchiveInOrganizer = "YES">
109+
</ArchiveAction>
110+
</Scheme>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>TouchEvents.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>2DE8D0021A64860B005C8B35</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
<key>2DE8D0171A64860B005C8B35</key>
21+
<dict>
22+
<key>primary</key>
23+
<true/>
24+
</dict>
25+
</dict>
26+
</dict>
27+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// AppDelegate.swift
3+
// chap12
4+
//
5+
// Created by PanaCloud on 7/22/14.
6+
// Copyright (c) 2014 PanaCloud. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
@UIApplicationMain
12+
class AppDelegate: UIResponder, UIApplicationDelegate {
13+
14+
var window: UIWindow?
15+
16+
17+
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
18+
// Override point for customization after application launch.
19+
20+
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
21+
22+
//Override point for customization after application launch
23+
var dvc = BNRDrawViewController()
24+
self.window!.rootViewController = dvc
25+
26+
self.window!.backgroundColor = UIColor.whiteColor()
27+
self.window!.makeKeyAndVisible()
28+
29+
return true
30+
}
31+
32+
func applicationWillResignActive(application: UIApplication!) {
33+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
34+
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
35+
}
36+
37+
func applicationDidEnterBackground(application: UIApplication!) {
38+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
39+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
40+
}
41+
42+
func applicationWillEnterForeground(application: UIApplication!) {
43+
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
44+
}
45+
46+
func applicationDidBecomeActive(application: UIApplication!) {
47+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
48+
}
49+
50+
func applicationWillTerminate(application: UIApplication!) {
51+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
52+
}
53+
54+
55+
}
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//
2+
// BNRDrawView.swift
3+
// chap12
4+
//
5+
// Created by PanaCloud on 7/22/14.
6+
// Copyright (c) 2014 PanaCloud. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class BNRDrawView: UIView, UIGestureRecognizerDelegate {
12+
13+
var finishedLines:NSMutableArray = NSMutableArray()
14+
//var currentLine:BNRLine?
15+
var linesInProgress: NSMutableDictionary = NSMutableDictionary()
16+
var selectedLine:BNRLine?
17+
18+
override init(frame: CGRect) {
19+
super.init(frame: frame)
20+
// Initialization code
21+
22+
23+
//finishedLines = NSMutableArray()
24+
//linesInProgress = NSMutableDictionary()
25+
26+
self.backgroundColor = UIColor.grayColor()
27+
self.multipleTouchEnabled = true
28+
29+
}
30+
31+
required init(coder aDecoder: NSCoder) {
32+
super.init(coder: aDecoder)
33+
}
34+
35+
36+
override func canBecomeFirstResponder() -> Bool {
37+
return true
38+
}
39+
40+
func strokeLine(line: BNRLine){
41+
var bp = UIBezierPath()
42+
bp.lineWidth = 10
43+
bp.lineCapStyle = kCGLineCapRound
44+
45+
bp.moveToPoint(line.begin)
46+
bp.addLineToPoint(line.end)
47+
bp.stroke()
48+
}
49+
50+
override func drawRect(rect: CGRect) {
51+
//Draw finished lines in black
52+
UIColor.blackColor().set()
53+
for(var line = 0; line < self.finishedLines.count; line++){
54+
self.strokeLine(self.finishedLines[line] as BNRLine)
55+
}
56+
//If there is a line currently beiing drawn, do it in red
57+
UIColor.redColor().set()
58+
for key in self.linesInProgress.allKeys as [NSValue] {
59+
self.strokeLine(self.linesInProgress[key] as BNRLine)
60+
}
61+
62+
}
63+
64+
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
65+
var t: AnyObject! = touches.anyObject()
66+
67+
println("Touch Began")
68+
//Get location of the touch in views coordinate system
69+
for t in touches.allObjects {
70+
var location = t.locationInView(self)
71+
var line = BNRLine()
72+
line.begin = location
73+
line.end = location
74+
75+
var key = NSValue(nonretainedObject: t)
76+
self.linesInProgress[key] = line
77+
}
78+
self.setNeedsDisplay()
79+
}
80+
81+
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
82+
var t: AnyObject! = touches.anyObject()
83+
84+
println("Touch Moved")
85+
86+
for t in touches.allObjects {
87+
var key = NSValue(nonretainedObject: t)
88+
var line:BNRLine = self.linesInProgress[key] as BNRLine
89+
line.end = t.locationInView(self)
90+
}
91+
92+
self.setNeedsDisplay()
93+
}
94+
95+
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
96+
var t: AnyObject! = touches.anyObject()
97+
98+
println("Touch Ended")
99+
100+
for t in touches.allObjects {
101+
var key = NSValue(nonretainedObject: t)
102+
var line:BNRLine = self.linesInProgress[key] as BNRLine
103+
104+
self.finishedLines.addObject(line)
105+
self.linesInProgress.removeObjectForKey(key)
106+
}
107+
self.setNeedsDisplay()
108+
}
109+
110+
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
111+
//Let's put in a log statement to see the order of events
112+
println("Touch Cancelled")
113+
var t: AnyObject! = touches.anyObject()
114+
115+
for t in touches.allObjects {
116+
var key = NSValue(nonretainedObject: t)
117+
self.linesInProgress.removeObjectForKey(key)
118+
}
119+
self.setNeedsDisplay()
120+
}
121+
122+
/*
123+
// Only override drawRect: if you perform custom drawing.
124+
// An empty implementation adversely affects performance during animation.
125+
override func drawRect(rect: CGRect)
126+
{
127+
// Drawing code
128+
}
129+
*/
130+
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// BNRDrawViewController.swift
3+
// chap12
4+
//
5+
// Created by PanaCloud on 7/22/14.
6+
// Copyright (c) 2014 PanaCloud. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class BNRDrawViewController: UIViewController {
12+
13+
override func viewDidLoad() {
14+
super.viewDidLoad()
15+
16+
// Do any additional setup after loading the view.
17+
}
18+
19+
override func didReceiveMemoryWarning() {
20+
super.didReceiveMemoryWarning()
21+
// Dispose of any resources that can be recreated.
22+
}
23+
24+
override func loadView() {
25+
self.view = BNRDrawView(frame: CGRectZero)
26+
27+
}
28+
29+
30+
/*
31+
// #pragma mark - Navigation
32+
33+
// In a storyboard-based application, you will often want to do a little preparation before navigation
34+
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
35+
// Get the new view controller using segue.destinationViewController.
36+
// Pass the selected object to the new view controller.
37+
}
38+
*/
39+
40+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// BNRLine.swift
3+
// chap12
4+
//
5+
// Created by PanaCloud on 7/22/14.
6+
// Copyright (c) 2014 PanaCloud. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class BNRLine: NSObject {
12+
var begin = CGPoint()
13+
var end = CGPoint()
14+
}

0 commit comments

Comments
 (0)