Skip to content

Commit

Permalink
add the Scratch Position in ScaratchCard
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeJoe authored and JoeJoe committed Dec 16, 2016
1 parent 8e83ce1 commit b74b828
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 11 deletions.
16 changes: 8 additions & 8 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
B4389DC21DFFF7B100B3DDE4 /* ExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4389DC11DFFF7B100B3DDE4 /* ExampleTests.swift */; };
B4389DDB1DFFFC4C00B3DDE4 /* image.jpg in Resources */ = {isa = PBXBuildFile; fileRef = B4389DD91DFFFC4C00B3DDE4 /* image.jpg */; };
B4389DDC1DFFFC4C00B3DDE4 /* mask.png in Resources */ = {isa = PBXBuildFile; fileRef = B4389DDA1DFFFC4C00B3DDE4 /* mask.png */; };
B4E3C0B61E04046000750E68 /* ScratchCard.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B4E3C0B51E04046000750E68 /* ScratchCard.framework */; };
B4E3C0B71E04046500750E68 /* ScratchCard.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B4E3C0B51E04046000750E68 /* ScratchCard.framework */; };
B4E3C0B81E04046500750E68 /* ScratchCard.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = B4E3C0B51E04046000750E68 /* ScratchCard.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
B4E5947A1E04615B00859958 /* ScratchCard.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B4E594791E04615B00859958 /* ScratchCard.framework */; };
B4E5947B1E04616400859958 /* ScratchCard.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B4E594791E04615B00859958 /* ScratchCard.framework */; };
B4E5947C1E04616400859958 /* ScratchCard.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = B4E594791E04615B00859958 /* ScratchCard.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -37,7 +37,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
B4E3C0B81E04046500750E68 /* ScratchCard.framework in Embed Frameworks */,
B4E5947C1E04616400859958 /* ScratchCard.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -57,16 +57,16 @@
B4389DC31DFFF7B100B3DDE4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
B4389DD91DFFFC4C00B3DDE4 /* image.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = image.jpg; sourceTree = "<group>"; };
B4389DDA1DFFFC4C00B3DDE4 /* mask.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = mask.png; sourceTree = "<group>"; };
B4E3C0B51E04046000750E68 /* ScratchCard.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = ScratchCard.framework; sourceTree = "<group>"; };
B4E594791E04615B00859958 /* ScratchCard.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = ScratchCard.framework; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
B4389DA61DFFF7B100B3DDE4 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
B4E3C0B61E04046000750E68 /* ScratchCard.framework in Frameworks */,
B4E3C0B71E04046500750E68 /* ScratchCard.framework in Frameworks */,
B4E5947A1E04615B00859958 /* ScratchCard.framework in Frameworks */,
B4E5947B1E04616400859958 /* ScratchCard.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -83,7 +83,7 @@
B4389DA01DFFF7B100B3DDE4 = {
isa = PBXGroup;
children = (
B4E3C0B51E04046000750E68 /* ScratchCard.framework */,
B4E594791E04615B00859958 /* ScratchCard.framework */,
B4389DAB1DFFF7B100B3DDE4 /* Example */,
B4389DC01DFFF7B100B3DDE4 /* ExampleTests */,
B4389DAA1DFFF7B100B3DDE4 /* Products */,
Expand Down
14 changes: 14 additions & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,31 @@ class ViewController: UIViewController, ScratchUIViewDelegate {
//Scratch Began Event(optional)
func scratchBegan(_ view: ScratchUIView) {
print("scratchBegan")

////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)

}

//Scratch Moved Event(optional)
func scratchMoved(_ view: ScratchUIView) {
let scratchPercent: Double = scratchCard.getScratchPercent()
textField.text = String(format: "%.2f", scratchPercent * 100) + "%"
print("scratchMoved")

////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)
}

//Scratch Ended Event(optional)
func scratchEnded(_ view: ScratchUIView) {
print("scratchEnded")

////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)

}
}
Binary file modified Example/ScratchCard.framework/Info.plist
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Example/ScratchCard.framework/ScratchCard
Binary file not shown.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To use ScratchCard in your project add the following 'Podfile' to your project
platform :ios, '8.0'
use_frameworks!

pod 'ScratchCard', '~> 1.0.9
pod 'ScratchCard', '~> 1.0.10

Then run:

Expand Down Expand Up @@ -74,6 +74,8 @@ Handle Scratch Event

It is easy to handle the scratch event(ScratchBegan, ScratchMoved, and ScratchEnded).

You can get the Scratch Position in the ScratchCard.

Please set the ScratchUIViewDelegate in your code.

* Sample:
Expand All @@ -93,18 +95,32 @@ class ViewController: UIViewController, ScratchUIViewDelegate {
//Scratch Began Event(optional)
func scratchBegan(_ view: ScratchUIView) {
print("scratchBegan")

////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)

}

//Scratch Moved Event(optional)
func scratchMoved(_ view: ScratchUIView) {
let scratchPercent: Double = scratchCard.getScratchPercent()
textField.text = String(format: "%.2f", scratchPercent * 100) + "%"
print("scratchMoved")

////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)
}

//Scratch Ended Event(optional)
func scratchEnded(_ view: ScratchUIView) {
print("scratchEnded")

////Get the Scratch Position in ScratchCard(coordinate origin is at the lower left corner)
let position = Int(view.scratchPosition.x).description + "," + Int(view.scratchPosition.y).description
print(position)

}
}
```
Expand Down
2 changes: 1 addition & 1 deletion ScratchCard.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ScratchCard"
s.version = "1.0.9"
s.version = "1.0.10"
s.license = "MIT"
s.summary = "A ScratchCard view on iOS(swift)."
s.homepage = "https://github.com/joehour/ScratchCard"
Expand Down
2 changes: 1 addition & 1 deletion ScratchCard/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.9</string>
<string>1.0.10</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
10 changes: 10 additions & 0 deletions ScratchCard/ScratchUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var uiScratchWidth: CGFloat!
open class ScratchUIView: UIView, ScratchViewDelegate {

open weak var delegate: ScratchUIViewDelegate!
open var scratchPosition: CGPoint!
override init(frame: CGRect) {
super.init(frame: frame)
self.Init()
Expand Down Expand Up @@ -63,6 +64,9 @@ open class ScratchUIView: UIView, ScratchViewDelegate {
guard self.delegate.scratchBegan != nil else {
return
}
if view.position.x >= 0 && view.position.x <= view.frame.width && view.position.y >= 0 && view.position.y <= view.frame.height {
scratchPosition = view.position
}
self.delegate.scratchBegan!(self)
}
}
Expand All @@ -72,6 +76,9 @@ open class ScratchUIView: UIView, ScratchViewDelegate {
guard self.delegate.scratchMoved != nil else {
return
}
if view.position.x >= 0 && view.position.x <= view.frame.width && view.position.y >= 0 && view.position.y <= view.frame.height {
scratchPosition = view.position
}
self.delegate.scratchMoved!(self)
}
}
Expand All @@ -81,6 +88,9 @@ open class ScratchUIView: UIView, ScratchViewDelegate {
guard self.delegate.scratchEnded != nil else {
return
}
if view.position.x >= 0 && view.position.x <= view.frame.width && view.position.y >= 0 && view.position.y <= view.frame.height {
scratchPosition = view.position
}
self.delegate.scratchEnded!(self)
}
}
Expand Down
7 changes: 7 additions & 0 deletions ScratchCard/ScratchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal protocol ScratchViewDelegate: class {
open class ScratchView: UIView {

internal weak var delegate: ScratchViewDelegate!
internal var position: CGPoint!
override init(frame: CGRect) {
super.init(frame: frame)
self.Init()
Expand Down Expand Up @@ -82,6 +83,8 @@ open class ScratchView: UIView {
firstTouch = true
location = CGPoint(x: touch.location(in: self).x, y: self.frame.size.height-touch.location(in: self).y)

position = location

if self.delegate != nil {
self.delegate.began(self)
}
Expand All @@ -100,6 +103,8 @@ open class ScratchView: UIView {
previousLocation = CGPoint(x: touch.previousLocation(in: self).x, y: self.frame.size.height-touch.previousLocation(in: self).y)
}

position = previousLocation

renderLineFromPoint(previousLocation, end: location)

if self.delegate != nil {
Expand All @@ -115,6 +120,8 @@ open class ScratchView: UIView {
firstTouch = false
previousLocation = CGPoint(x: touch.previousLocation(in: self).x, y: self.frame.size.height-touch.previousLocation(in: self).y)

position = previousLocation

renderLineFromPoint(previousLocation, end: location)

if self.delegate != nil {
Expand Down

0 comments on commit b74b828

Please sign in to comment.