Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
4089aa47
Commit
4089aa47
authored
Feb 26, 2016
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed MaterialSwitch handling for touchesBegan
parent
66f04237
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
12 deletions
+31
-12
Sources/MaterialSwitch.swift
+31
-12
No files found.
Sources/MaterialSwitch.swift
View file @
4089aa47
...
...
@@ -280,6 +280,7 @@ public class MaterialSwitch: UIControl {
trackLayer
=
MaterialLayer
()
button
=
FabButton
()
super
.
init
(
coder
:
aDecoder
)
prepareView
()
prepareTrack
()
prepareButton
()
prepareSwitchSize
(
.
Default
)
...
...
@@ -297,6 +298,7 @@ public class MaterialSwitch: UIControl {
trackLayer
=
MaterialLayer
()
button
=
FabButton
()
super
.
init
(
frame
:
CGRectNull
)
prepareView
()
prepareTrack
()
prepareButton
()
prepareSwitchSize
(
size
)
...
...
@@ -304,6 +306,17 @@ public class MaterialSwitch: UIControl {
prepareSwitchState
(
state
)
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
public
func
prepareView
()
{
}
public
override
func
willMoveToSuperview
(
newSuperview
:
UIView
?)
{
super
.
willMoveToSuperview
(
newSuperview
)
styleForState
(
internalSwitchState
)
...
...
@@ -372,14 +385,14 @@ public class MaterialSwitch: UIControl {
- Parameter sender: A UIButton.
- Parameter event: A UIEvent.
*/
//
internal func handleTouchUpOutsideOrCanceled(sender: FabButton, event: UIEvent) {
//
if let v: UITouch = event.touchesForView(sender)?.first {
//
let t: CGPoint = v.previousLocationInView(sender)
//
let p: CGPoint = v.locationInView(sender)
//
let q: CGFloat = sender.x + p.x - t.x
//
setSwitchState(q > (width - button.width) / 2 ? .On : .Off, animated: true)
//
}
//
}
internal
func
handleTouchUpOutsideOrCanceled
(
sender
:
FabButton
,
event
:
UIEvent
)
{
if
let
v
:
UITouch
=
event
.
touchesForView
(
sender
)?
.
first
{
let
t
:
CGPoint
=
v
.
previousLocationInView
(
sender
)
let
p
:
CGPoint
=
v
.
locationInView
(
sender
)
let
q
:
CGFloat
=
sender
.
x
+
p
.
x
-
t
.
x
setSwitchState
(
q
>
(
width
-
button
.
width
)
/
2
?
.
On
:
.
Off
,
animated
:
true
)
}
}
/**
Handle the TouchDragInside event.
...
...
@@ -397,6 +410,12 @@ public class MaterialSwitch: UIControl {
}
}
public
override
func
touchesBegan
(
touches
:
Set
<
UITouch
>
,
withEvent
event
:
UIEvent
?)
{
if
true
==
CGRectContainsPoint
(
trackLayer
.
frame
,
layer
.
convertPoint
(
touches
.
first
!.
locationInView
(
self
),
fromLayer
:
layer
))
{
setSwitchState
(
.
On
==
internalSwitchState
?
.
Off
:
.
On
)
}
}
/// Prepares the track.
private
func
prepareTrack
()
{
layer
.
addSublayer
(
trackLayer
)
...
...
@@ -405,10 +424,10 @@ public class MaterialSwitch: UIControl {
/// Prepares the button.
private
func
prepareButton
()
{
button
.
pulseColor
=
nil
//
button.addTarget(self, action: "handleTouchUpOutsideOrCanceled:event:", forControlEvents: .TouchUpOutside)
//
button.addTarget(self, action: "handleTouchUpInside", forControlEvents: .TouchUpInside)
//
button.addTarget(self, action: "handleTouchDragInside:event:", forControlEvents: .TouchDragInside)
//
button.addTarget(self, action: "handleTouchUpOutsideOrCanceled:event:", forControlEvents: .TouchCancel)
button
.
addTarget
(
self
,
action
:
"handleTouchUpOutsideOrCanceled:event:"
,
forControlEvents
:
.
TouchUpOutside
)
button
.
addTarget
(
self
,
action
:
"handleTouchUpInside"
,
forControlEvents
:
.
TouchUpInside
)
button
.
addTarget
(
self
,
action
:
"handleTouchDragInside:event:"
,
forControlEvents
:
.
TouchDragInside
)
button
.
addTarget
(
self
,
action
:
"handleTouchUpOutsideOrCanceled:event:"
,
forControlEvents
:
.
TouchCancel
)
addSubview
(
button
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment