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
ae8f95f3
Commit
ae8f95f3
authored
Aug 26, 2015
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
progressive cleanup
parent
b04f0258
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
157 additions
and
97 deletions
+157
-97
Source/FabButton.swift
+50
-97
Source/MaterialButton.swift
+107
-0
No files found.
Source/FabButton.swift
View file @
ae8f95f3
...
@@ -19,38 +19,49 @@
...
@@ -19,38 +19,49 @@
import
UIKit
import
UIKit
public
class
FabButton
:
MaterialButton
{
public
class
FabButton
:
MaterialButton
{
/**
var
lineWidth
:
CGFloat
=
2.0
:name: lineWidth
*/
public
var
lineWidth
:
CGFloat
=
2.0
/**
:name: init
*/
public
required
init
(
coder
aDecoder
:
NSCoder
)
{
super
.
init
(
coder
:
aDecoder
)
initialize
()
}
public
convenience
init
()
{
/**
self
.
init
(
frame
:
CGRectZero
)
:name: init
*/
public
required
init
(
frame
:
CGRect
)
{
super
.
init
(
frame
:
frame
)
initialize
()
}
}
/**
:name: drawRect
*/
public
override
func
drawRect
(
rect
:
CGRect
)
{
public
override
func
drawRect
(
rect
:
CGRect
)
{
setupContext
(
rect
)
prepareContext
(
rect
)
setupBackgroundColorView
()
prepareBackgroundColorView
()
setupPlus
()
preparePlus
()
}
public
required
init
(
coder
aDecoder
:
NSCoder
)
{
super
.
init
(
coder
:
aDecoder
)
initialize
()
applyShadow
()
}
public
required
override
init
(
frame
:
CGRect
)
{
super
.
init
(
frame
:
frame
)
initialize
()
applyShadow
()
}
}
func
initialize
()
{
//
color
=
UIColor
.
redColor
()
// :name: initialize
pulseColor
=
UIColor
.
whiteColor
()
//
internal
func
initialize
()
{
color
=
.
redColor
()
pulseColor
=
.
whiteColor
()
setTranslatesAutoresizingMaskIntoConstraints
(
false
)
setTranslatesAutoresizingMaskIntoConstraints
(
false
)
}
}
func
setupContext
(
rect
:
CGRect
)
{
//
// :name: prepareContext
//
private
func
prepareContext
(
rect
:
CGRect
)
{
let
context
=
UIGraphicsGetCurrentContext
()
let
context
=
UIGraphicsGetCurrentContext
()
CGContextSaveGState
(
context
);
CGContextSaveGState
(
context
);
CGContextAddEllipseInRect
(
context
,
rect
)
CGContextAddEllipseInRect
(
context
,
rect
)
...
@@ -58,10 +69,13 @@ public class FabButton : MaterialButton {
...
@@ -58,10 +69,13 @@ public class FabButton : MaterialButton {
CGContextFillPath
(
context
)
CGContextFillPath
(
context
)
CGContextRestoreGState
(
context
);
CGContextRestoreGState
(
context
);
}
}
//
// :name: prepareBackgroundColorView
//
// We need this view so we can use the masksToBounds
// We need this view so we can use the masksToBounds
// so the pulse doesn't animate off the button
// so the pulse doesn't animate off the button
func
setup
BackgroundColorView
()
{
func
prepare
BackgroundColorView
()
{
backgroundColorView
=
UIView
()
backgroundColorView
=
UIView
()
backgroundColorView
!.
frame
=
bounds
backgroundColorView
!.
frame
=
bounds
backgroundColorView
!.
layer
.
cornerRadius
=
bounds
.
width
/
2.0
backgroundColorView
!.
layer
.
cornerRadius
=
bounds
.
width
/
2.0
...
@@ -70,90 +84,29 @@ public class FabButton : MaterialButton {
...
@@ -70,90 +84,29 @@ public class FabButton : MaterialButton {
backgroundColorView
!.
userInteractionEnabled
=
false
backgroundColorView
!.
userInteractionEnabled
=
false
insertSubview
(
backgroundColorView
!
,
atIndex
:
0
)
insertSubview
(
backgroundColorView
!
,
atIndex
:
0
)
}
}
//
// :name: preparePlus
//
// I make the + with two views because
// I make the + with two views because
// The label is not actually vertically and horizontally aligned
// The label is not actually vertically and horizontally aligned
// Quick hack instead of subclassing UILabel and override drawTextInRect
// Quick hack instead of subclassing UILabel and override drawTextInRect
func
setup
Plus
()
{
private
func
prepare
Plus
()
{
setup
VerticalLine
()
prepare
VerticalLine
()
setup
HorizontalLine
()
prepare
HorizontalLine
()
}
}
func
setup
VerticalLine
()
{
private
func
prepare
VerticalLine
()
{
verticalLine
=
UIView
(
frame
:
CGRectMake
(
0
,
0
,
lineWidth
,
CGRectGetHeight
(
backgroundColorView
!.
frame
)
/
3.0
))
verticalLine
=
UIView
(
frame
:
CGRectMake
(
0
,
0
,
lineWidth
,
CGRectGetHeight
(
backgroundColorView
!.
frame
)
/
3.0
))
verticalLine
!.
backgroundColor
=
UIColor
.
whiteColor
()
verticalLine
!.
backgroundColor
=
UIColor
.
whiteColor
()
verticalLine
!.
center
=
backgroundColorView
!.
center
verticalLine
!.
center
=
backgroundColorView
!.
center
backgroundColorView
!.
addSubview
(
verticalLine
!
)
backgroundColorView
!.
addSubview
(
verticalLine
!
)
}
}
func
setup
HorizontalLine
()
{
private
func
prepare
HorizontalLine
()
{
horizontalLine
=
UIView
(
frame
:
CGRectMake
(
0
,
0
,
CGRectGetWidth
(
backgroundColorView
!.
frame
)
/
3.0
,
lineWidth
))
horizontalLine
=
UIView
(
frame
:
CGRectMake
(
0
,
0
,
CGRectGetWidth
(
backgroundColorView
!.
frame
)
/
3.0
,
lineWidth
))
horizontalLine
!.
backgroundColor
=
UIColor
.
whiteColor
()
horizontalLine
!.
backgroundColor
=
UIColor
.
whiteColor
()
horizontalLine
!.
center
=
backgroundColorView
!.
center
horizontalLine
!.
center
=
backgroundColorView
!.
center
backgroundColorView
!.
addSubview
(
horizontalLine
!
)
backgroundColorView
!.
addSubview
(
horizontalLine
!
)
}
}
func
applyShadow
()
{
layer
.
shadowOffset
=
CGSizeMake
(
1
,
1
)
layer
.
shadowColor
=
UIColor
.
blackColor
()
.
CGColor
layer
.
shadowOpacity
=
0.5
layer
.
shadowRadius
=
5
}
public
override
func
touchesBegan
(
touches
:
Set
<
NSObject
>
,
withEvent
event
:
UIEvent
)
{
super
.
touchesBegan
(
touches
,
withEvent
:
event
)
pulseTouches
(
touches
)
}
public
override
func
touchesEnded
(
touches
:
Set
<
NSObject
>
,
withEvent
event
:
UIEvent
)
{
super
.
touchesEnded
(
touches
,
withEvent
:
event
)
shrink
()
removePulse
()
}
public
override
func
touchesCancelled
(
touches
:
Set
<
NSObject
>!
,
withEvent
event
:
UIEvent
!
)
{
super
.
touchesCancelled
(
touches
,
withEvent
:
event
)
shrink
()
removePulse
()
}
func
pulseTouches
(
touches
:
NSSet
)
{
let
touch
=
touches
.
allObjects
.
last
as!
UITouch
let
touchLocation
=
touch
.
locationInView
(
self
)
pulseView
=
UIView
()
pulseView
!.
frame
=
CGRectMake
(
0
,
0
,
bounds
.
width
,
bounds
.
height
)
pulseView
!.
layer
.
cornerRadius
=
bounds
.
width
/
2.0
pulseView
!.
center
=
touchLocation
pulseView
!.
backgroundColor
=
pulseColor
!.
colorWithAlphaComponent
(
0.5
)
backgroundColorView
!.
addSubview
(
pulseView
!
)
UIView
.
animateWithDuration
(
0.3
,
animations
:
{
self
.
pulseView
!.
transform
=
CGAffineTransformMakeScale
(
3
,
3
)
self
.
transform
=
CGAffineTransformMakeScale
(
1.1
,
1.1
)
},
completion
:
nil
)
}
func
shrink
()
{
UIView
.
animateWithDuration
(
0.3
,
delay
:
0.0
,
usingSpringWithDamping
:
0.2
,
initialSpringVelocity
:
10
,
options
:
nil
,
animations
:
{
self
.
transform
=
CGAffineTransformIdentity
},
completion
:
nil
)
}
func
removePulse
()
{
UIView
.
animateWithDuration
(
0.3
,
animations
:
{
_
in
self
.
pulseView
?
.
alpha
=
0.0
})
{
(
finished
)
->
Void
in
self
.
pulseView
?
.
removeFromSuperview
()
self
.
pulseView
=
nil
}
}
}
}
Source/MaterialButton.swift
View file @
ae8f95f3
...
@@ -26,4 +26,111 @@ public class MaterialButton : UIButton {
...
@@ -26,4 +26,111 @@ public class MaterialButton : UIButton {
internal
var
verticalLine
:
UIView
?
internal
var
verticalLine
:
UIView
?
internal
var
horizontalLine
:
UIView
?
internal
var
horizontalLine
:
UIView
?
internal
var
backgroundColorView
:
UIView
?
internal
var
backgroundColorView
:
UIView
?
/**
:name: init
*/
public
required
init
(
coder
aDecoder
:
NSCoder
)
{
super
.
init
(
coder
:
aDecoder
)
prepareShadow
()
}
/**
:name: init
*/
public
required
override
init
(
frame
:
CGRect
)
{
super
.
init
(
frame
:
frame
)
prepareShadow
()
}
/**
:name: init
*/
public
convenience
init
()
{
self
.
init
(
frame
:
CGRectZero
)
}
func
prepareShadow
()
{
layer
.
shadowOffset
=
CGSizeMake
(
1
,
1
)
layer
.
shadowColor
=
UIColor
.
blackColor
()
.
CGColor
layer
.
shadowOpacity
=
0.5
layer
.
shadowRadius
=
5
}
/**
:name: touchesBegan
*/
public
override
func
touchesBegan
(
touches
:
Set
<
NSObject
>
,
withEvent
event
:
UIEvent
)
{
super
.
touchesBegan
(
touches
,
withEvent
:
event
)
pulseTouches
(
touches
)
}
/**
:name: touchesEnded
*/
public
override
func
touchesEnded
(
touches
:
Set
<
NSObject
>
,
withEvent
event
:
UIEvent
)
{
super
.
touchesEnded
(
touches
,
withEvent
:
event
)
shrink
()
removePulse
()
}
/**
:name: touchesCancelled
*/
public
override
func
touchesCancelled
(
touches
:
Set
<
NSObject
>!
,
withEvent
event
:
UIEvent
!
)
{
super
.
touchesCancelled
(
touches
,
withEvent
:
event
)
shrink
()
removePulse
()
}
//
// :name: pulseTouches
//
internal
func
pulseTouches
(
touches
:
NSSet
)
{
let
touch
=
touches
.
allObjects
.
last
as!
UITouch
let
touchLocation
=
touch
.
locationInView
(
self
)
pulseView
=
UIView
()
pulseView
!.
frame
=
CGRectMake
(
0
,
0
,
bounds
.
width
,
bounds
.
height
)
pulseView
!.
layer
.
cornerRadius
=
bounds
.
width
/
2.0
pulseView
!.
center
=
touchLocation
pulseView
!.
backgroundColor
=
pulseColor
!.
colorWithAlphaComponent
(
0.5
)
backgroundColorView
!.
addSubview
(
pulseView
!
)
UIView
.
animateWithDuration
(
0.3
,
animations
:
{
self
.
pulseView
!.
transform
=
CGAffineTransformMakeScale
(
3
,
3
)
self
.
transform
=
CGAffineTransformMakeScale
(
1.1
,
1.1
)
},
completion
:
nil
)
}
//
// :name: shrink
//
internal
func
shrink
()
{
UIView
.
animateWithDuration
(
0.3
,
delay
:
0.0
,
usingSpringWithDamping
:
0.2
,
initialSpringVelocity
:
10
,
options
:
nil
,
animations
:
{
self
.
transform
=
CGAffineTransformIdentity
},
completion
:
nil
)
}
//
// :name: removePulse
//
internal
func
removePulse
()
{
UIView
.
animateWithDuration
(
0.3
,
animations
:
{
_
in
self
.
pulseView
?
.
alpha
=
0.0
}
)
{
_
in
self
.
pulseView
?
.
removeFromSuperview
()
self
.
pulseView
=
nil
}
}
}
}
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