Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nghialv committed Mar 13, 2015
1 parent 7b0db89 commit f0f1cef
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 67 deletions.
6 changes: 3 additions & 3 deletions Example/MaterialKit/BarButtonItemViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class BarButtonItemViewController: UIViewController {
imgView.image = UIImage(named: "uibaritem_icon.png")
imgView.backgroundAniEnabled = false
imgView.rippleLocation = .Center
imgView.circleGrowRatioMax = 1.15
imgView.ripplePercent = 1.15
imgView.userInteractionEnabled = true

let rightButton = UIBarButtonItem(customView: imgView)
self.navigationItem.rightBarButtonItem = rightButton


label.rippleLocation = .TapLocation
label.circleLayerColor = UIColor.MKColor.LightGreen
label.rippleLayerColor = UIColor.MKColor.LightGreen
label.backgroundLayerColor = UIColor.clearColor()
//label.userInteractionEnabled = true
NSTimer.scheduledTimerWithTimeInterval(2.5, target: self, selector: "animateLabelRipple", userInfo: nil, repeats: false)

imageView.layer.borderColor = UIColor.MKColor.Grey.CGColor
imageView.layer.borderWidth = 1.0
imageView.circleGrowRatioMax = 1.2
imageView.ripplePercent = 1.2
imageView.rippleLocation = .Left
NSTimer.scheduledTimerWithTimeInterval(3.5, target: self, selector: "animateImageRipple", userInfo: nil, repeats: false)
}
Expand Down
8 changes: 4 additions & 4 deletions Example/MaterialKit/ButtonViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ class ButtonViewController: UIViewController {
flatButton1.layer.shadowOffset = CGSize(width: 0, height: 2.5)

flatButton2.maskEnabled = false
flatButton2.circleGrowRatioMax = 0.5
flatButton2.ripplePercent = 0.5
flatButton2.backgroundAniEnabled = false
flatButton2.rippleLocation = .Center

imageButton1.circleLayerColor = UIColor.MKColor.DeepOrange
imageButton1.rippleLayerColor = UIColor.MKColor.DeepOrange

imageButton2.maskEnabled = false
imageButton2.circleGrowRatioMax = 1.2
imageButton2.ripplePercent = 1.2
imageButton2.backgroundAniEnabled = false
imageButton2.rippleLocation = .Center

floatButton1.cornerRadius = 40.0
floatButton1.backgroundLayerCornerRadius = 40.0
floatButton1.maskEnabled = false
floatButton1.circleGrowRatioMax = 1.75
floatButton1.ripplePercent = 1.75
floatButton1.rippleLocation = .Center
floatButton1.aniDuration = 0.85

Expand Down
2 changes: 1 addition & 1 deletion Example/MaterialKit/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TableViewController: UIViewController, UITableViewDelegate, UITableViewDat
cell.rippleLocation = rippleLocations[indexPath.row]

let index = indexPath.row % circleColors.count
cell.circleLayerColor = circleColors[index]
cell.rippleLayerColor = circleColors[index]

return cell
}
Expand Down
6 changes: 3 additions & 3 deletions Example/MaterialKit/TextFieldViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TextFieldViewController: UIViewController {

// Border, no shadow, floatPlaceHolderDisabled
textField3.layer.borderColor = UIColor.MKColor.Grey.CGColor
textField3.circleLayerColor = UIColor.MKColor.Amber
textField3.rippleLayerColor = UIColor.MKColor.Amber
textField3.tintColor = UIColor.MKColor.DeepOrange
textField3.rippleLocation = .Left

Expand All @@ -48,7 +48,7 @@ class TextFieldViewController: UIViewController {
textField5.layer.borderColor = UIColor.clearColor().CGColor
textField5.floatingPlaceholderEnabled = true
textField5.placeholder = "Email account"
textField5.circleLayerColor = UIColor.MKColor.LightBlue
textField5.rippleLayerColor = UIColor.MKColor.LightBlue
textField5.tintColor = UIColor.MKColor.Blue
textField5.backgroundColor = UIColor(hex: 0xE0E0E0)

Expand All @@ -57,7 +57,7 @@ class TextFieldViewController: UIViewController {
textField6.cornerRadius = 1.0
textField6.placeholder = "Description"
textField6.layer.borderColor = UIColor.MKColor.Green.CGColor
textField6.circleLayerColor = UIColor.MKColor.LightGreen
textField6.rippleLayerColor = UIColor.MKColor.LightGreen
textField6.tintColor = UIColor.MKColor.LightGreen
}

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Components
let button = MKButton(frame: CGRect(x: 10, y: 10, width: 100, height: 35))
button.maskEnabled = true
button.rippleLocation = .TapLocation
button.circleLayerColor = UIColor.MKColor.LightGreen
button.rippleLayerColor = UIColor.MKColor.LightGreen
```

#### MKTextField
Expand All @@ -68,7 +68,7 @@ Components
textField.floatingPlaceholderEnabled = true
textField.placeholder = "Description"
textField.layer.borderColor = UIColor.MKColor.Green.CGColor
textField.circleLayerColor = UIColor.MKColor.LightGreen
textField.rippleLayerColor = UIColor.MKColor.LightGreen
```

#### MKTableViewCell
Expand All @@ -81,7 +81,7 @@ Components
``` swift
var cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as MyCell
cell.rippleLocation = .Center
cell.circleLayerColor = UIColor.MKColor.Blue
cell.rippleLayerColor = UIColor.MKColor.Blue
```

#### MKLabel, MKImageView (BarButtonItem)
Expand Down
16 changes: 8 additions & 8 deletions Source/MKButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class MKButton : UIButton
mkLayer.rippleLocation = rippleLocation
}
}
@IBInspectable public var circleGrowRatioMax: Float = 0.9 {
@IBInspectable public var ripplePercent: Float = 0.9 {
didSet {
mkLayer.circleGrowRatioMax = circleGrowRatioMax
mkLayer.ripplePercent = ripplePercent
}
}
@IBInspectable public var backgroundLayerCornerRadius: CGFloat = 0.0 {
Expand All @@ -41,7 +41,7 @@ public class MKButton : UIButton
}
}
@IBInspectable public var aniDuration: Float = 0.65
@IBInspectable public var circleAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var rippleAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var backgroundAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var shadowAniTimingFunction: MKTimingFunction = .EaseOut

Expand All @@ -52,9 +52,9 @@ public class MKButton : UIButton
}
}
// color
@IBInspectable public var circleLayerColor: UIColor = UIColor(white: 0.45, alpha: 0.5) {
@IBInspectable public var rippleLayerColor: UIColor = UIColor(white: 0.45, alpha: 0.5) {
didSet {
mkLayer.setCircleLayerColor(circleLayerColor)
mkLayer.setCircleLayerColor(rippleLayerColor)
}
}
@IBInspectable public var backgroundLayerColor: UIColor = UIColor(white: 0.75, alpha: 0.25) {
Expand Down Expand Up @@ -86,7 +86,7 @@ public class MKButton : UIButton
adjustsImageWhenHighlighted = false
cornerRadius = 2.5
mkLayer.setBackgroundLayerColor(backgroundLayerColor)
mkLayer.setCircleLayerColor(circleLayerColor)
mkLayer.setCircleLayerColor(rippleLayerColor)
}

// MARK - location tracking methods
Expand All @@ -95,8 +95,8 @@ public class MKButton : UIButton
mkLayer.didChangeTapLocation(touch.locationInView(self))
}

// circleLayer animation
mkLayer.animateScaleForCircleLayer(0.45, toScale: 1.0, timingFunction: circleAniTimingFunction, duration: CFTimeInterval(aniDuration))
// rippleLayer animation
mkLayer.animateScaleForCircleLayer(0.45, toScale: 1.0, timingFunction: rippleAniTimingFunction, duration: CFTimeInterval(aniDuration))

// backgroundLayer animation
if backgroundAniEnabled {
Expand Down
14 changes: 7 additions & 7 deletions Source/MKImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MKImageView: UIImageView
}
}
@IBInspectable public var aniDuration: Float = 0.65
@IBInspectable public var circleAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var rippleAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var backgroundAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var backgroundAniEnabled: Bool = true {
didSet {
Expand All @@ -31,9 +31,9 @@ public class MKImageView: UIImageView
}
}
}
@IBInspectable public var circleGrowRatioMax: Float = 0.9 {
@IBInspectable public var ripplePercent: Float = 0.9 {
didSet {
mkLayer.circleGrowRatioMax = circleGrowRatioMax
mkLayer.ripplePercent = ripplePercent
}
}

Expand All @@ -44,9 +44,9 @@ public class MKImageView: UIImageView
}
}
// color
@IBInspectable public var circleLayerColor: UIColor = UIColor(white: 0.45, alpha: 0.5) {
@IBInspectable public var rippleLayerColor: UIColor = UIColor(white: 0.45, alpha: 0.5) {
didSet {
mkLayer.setCircleLayerColor(circleLayerColor)
mkLayer.setCircleLayerColor(rippleLayerColor)
}
}
@IBInspectable public var backgroundLayerColor: UIColor = UIColor(white: 0.75, alpha: 0.25) {
Expand Down Expand Up @@ -87,7 +87,7 @@ public class MKImageView: UIImageView
}

private func setup() {
mkLayer.setCircleLayerColor(circleLayerColor)
mkLayer.setCircleLayerColor(rippleLayerColor)
mkLayer.setBackgroundLayerColor(backgroundLayerColor)
mkLayer.setMaskLayerCornerRadius(cornerRadius)
}
Expand All @@ -99,7 +99,7 @@ public class MKImageView: UIImageView
rippleLocation = .Center
}

mkLayer.animateScaleForCircleLayer(0.65, toScale: 1.0, timingFunction: circleAniTimingFunction, duration: CFTimeInterval(aniDuration))
mkLayer.animateScaleForCircleLayer(0.65, toScale: 1.0, timingFunction: rippleAniTimingFunction, duration: CFTimeInterval(aniDuration))
mkLayer.animateAlphaForBackgroundLayer(backgroundAniTimingFunction, duration: CFTimeInterval(aniDuration))
}

Expand Down
14 changes: 7 additions & 7 deletions Source/MKLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MKLabel: UILabel {
}
}
@IBInspectable public var aniDuration: Float = 0.65
@IBInspectable public var circleAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var rippleAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var backgroundAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var backgroundAniEnabled: Bool = true {
didSet {
Expand All @@ -29,9 +29,9 @@ public class MKLabel: UILabel {
}
}
}
@IBInspectable public var circleGrowRatioMax: Float = 0.9 {
@IBInspectable public var ripplePercent: Float = 0.9 {
didSet {
mkLayer.circleGrowRatioMax = circleGrowRatioMax
mkLayer.ripplePercent = ripplePercent
}
}

Expand All @@ -42,9 +42,9 @@ public class MKLabel: UILabel {
}
}
// color
@IBInspectable public var circleLayerColor: UIColor = UIColor(white: 0.45, alpha: 0.5) {
@IBInspectable public var rippleLayerColor: UIColor = UIColor(white: 0.45, alpha: 0.5) {
didSet {
mkLayer.setCircleLayerColor(circleLayerColor)
mkLayer.setCircleLayerColor(rippleLayerColor)
}
}
@IBInspectable public var backgroundLayerColor: UIColor = UIColor(white: 0.75, alpha: 0.25) {
Expand Down Expand Up @@ -75,7 +75,7 @@ public class MKLabel: UILabel {
}

private func setup() {
mkLayer.setCircleLayerColor(circleLayerColor)
mkLayer.setCircleLayerColor(rippleLayerColor)
mkLayer.setBackgroundLayerColor(backgroundLayerColor)
mkLayer.setMaskLayerCornerRadius(cornerRadius)
}
Expand All @@ -87,7 +87,7 @@ public class MKLabel: UILabel {
rippleLocation = .Center
}

mkLayer.animateScaleForCircleLayer(0.65, toScale: 1.0, timingFunction: circleAniTimingFunction, duration: CFTimeInterval(aniDuration))
mkLayer.animateScaleForCircleLayer(0.65, toScale: 1.0, timingFunction: rippleAniTimingFunction, duration: CFTimeInterval(aniDuration))
mkLayer.animateAlphaForBackgroundLayer(backgroundAniTimingFunction, duration: CFTimeInterval(aniDuration))
}

Expand Down
38 changes: 19 additions & 19 deletions Source/MKLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public enum MKRippleLocation {

public class MKLayer {
private var superLayer: CALayer!
private let circleLayer = CALayer()
private let rippleLayer = CALayer()
private let backgroundLayer = CALayer()
private let maskLayer = CAShapeLayer()
public var rippleLocation: MKRippleLocation = .TapLocation {
Expand All @@ -60,15 +60,15 @@ public class MKLayer {
}
}

public var circleGrowRatioMax: Float = 0.9 {
public var ripplePercent: Float = 0.9 {
didSet {
if circleGrowRatioMax > 0 {
if ripplePercent > 0 {
let superLayerWidth = CGRectGetWidth(superLayer.bounds)
let superLayerHeight = CGRectGetHeight(superLayer.bounds)
let circleSize = CGFloat(max(superLayerWidth, superLayerHeight)) * CGFloat(circleGrowRatioMax)
let circleSize = CGFloat(max(superLayerWidth, superLayerHeight)) * CGFloat(ripplePercent)
let circleCornerRadius = circleSize/2

circleLayer.cornerRadius = circleCornerRadius
rippleLayer.cornerRadius = circleCornerRadius
setCircleLayerLocationAt(CGPoint(x: superLayerWidth/2, y: superLayerHeight/2))
}
}
Expand All @@ -86,13 +86,13 @@ public class MKLayer {
superLayer.addSublayer(backgroundLayer)

// circlelayer
let circleSize = CGFloat(max(superLayerWidth, superLayerHeight)) * CGFloat(circleGrowRatioMax)
let circleSize = CGFloat(max(superLayerWidth, superLayerHeight)) * CGFloat(ripplePercent)
let circleCornerRadius = circleSize/2

circleLayer.opacity = 0.0
circleLayer.cornerRadius = circleCornerRadius
rippleLayer.opacity = 0.0
rippleLayer.cornerRadius = circleCornerRadius
setCircleLayerLocationAt(CGPoint(x: superLayerWidth/2, y: superLayerHeight/2))
backgroundLayer.addSublayer(circleLayer)
backgroundLayer.addSublayer(rippleLayer)

// mask layer
setMaskLayerCornerRadius(superLayer.cornerRadius)
Expand All @@ -110,15 +110,15 @@ public class MKLayer {

public func enableOnlyCircleLayer() {
backgroundLayer.removeFromSuperlayer()
superLayer.addSublayer(circleLayer)
superLayer.addSublayer(rippleLayer)
}

public func setBackgroundLayerColor(color: UIColor) {
backgroundLayer.backgroundColor = color.CGColor
}

public func setCircleLayerColor(color: UIColor) {
circleLayer.backgroundColor = color.CGColor
rippleLayer.backgroundColor = color.CGColor
}

public func didChangeTapLocation(location: CGPoint) {
Expand All @@ -143,23 +143,23 @@ public class MKLayer {
let bounds = superLayer.bounds
let width = CGRectGetWidth(bounds)
let height = CGRectGetHeight(bounds)
let subSize = CGFloat(max(width, height)) * CGFloat(circleGrowRatioMax)
let subSize = CGFloat(max(width, height)) * CGFloat(ripplePercent)
let subX = center.x - subSize/2
let subY = center.y - subSize/2

// disable animation when changing layer frame
CATransaction.begin()
CATransaction.setDisableActions(true)
circleLayer.cornerRadius = subSize / 2
circleLayer.frame = CGRect(x: subX, y: subY, width: subSize, height: subSize)
rippleLayer.cornerRadius = subSize / 2
rippleLayer.frame = CGRect(x: subX, y: subY, width: subSize, height: subSize)
CATransaction.commit()
}

// MARK - Animation
public func animateScaleForCircleLayer(fromScale: Float, toScale: Float, timingFunction: MKTimingFunction, duration: CFTimeInterval) {
let circleLayerAnim = CABasicAnimation(keyPath: "transform.scale")
circleLayerAnim.fromValue = fromScale
circleLayerAnim.toValue = toScale
let rippleLayerAnim = CABasicAnimation(keyPath: "transform.scale")
rippleLayerAnim.fromValue = fromScale
rippleLayerAnim.toValue = toScale

let opacityAnim = CABasicAnimation(keyPath: "opacity")
opacityAnim.fromValue = 1.0
Expand All @@ -171,9 +171,9 @@ public class MKLayer {
groupAnim.removedOnCompletion = false
groupAnim.fillMode = kCAFillModeForwards

groupAnim.animations = [circleLayerAnim, opacityAnim]
groupAnim.animations = [rippleLayerAnim, opacityAnim]

circleLayer.addAnimation(groupAnim, forKey: nil)
rippleLayer.addAnimation(groupAnim, forKey: nil)
}

public func animateAlphaForBackgroundLayer(timingFunction: MKTimingFunction, duration: CFTimeInterval) {
Expand Down
Loading

0 comments on commit f0f1cef

Please sign in to comment.