Skip to content

Commit

Permalink
Added class and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zoonooz committed Jun 28, 2014
1 parent 6a7cff7 commit 4a9373f
Show file tree
Hide file tree
Showing 11 changed files with 890 additions and 7 deletions.
30 changes: 23 additions & 7 deletions .gitignore
@@ -1,8 +1,24 @@
# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control?
#
# Pods/
# Mac
.DS_Store

# Xcode
build/*
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
*.xcworkspace
!default.xcworkspace
xcuserdata
profile
*.moved-aside

xcuserdata/
project.xcworkspace/

xcshareddata/

148 changes: 148 additions & 0 deletions Classes/ZFRippleButton.swift
@@ -0,0 +1,148 @@
//
// ZFRippleButton.swift
// ZFRippleButtonDemo
//
// Created by Amornchai Kanokpullwad on 6/26/14.
// Copyright (c) 2014 zoonref. All rights reserved.
//

import UIKit
import QuartzCore

class ZFRippleButton: UIButton {

@IBInspectable var rippleOverBounds: Bool = false {
didSet {
if rippleOverBounds {
rippleBackgroundView.layer.mask = nil
} else {
var maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: layer.cornerRadius).CGPath
rippleBackgroundView.layer.mask = maskLayer
}
}
}
@IBInspectable var rippleColor: UIColor = UIColor(white: 0.9, alpha: 1) {
didSet {
rippleView.backgroundColor = rippleColor
}
}
@IBInspectable var rippleBackgroundColor: UIColor = UIColor(white: 0.95, alpha: 1) {
didSet {
rippleBackgroundView.backgroundColor = rippleBackgroundColor
}
}
@IBInspectable var shadowRippleRadius: CGFloat = 1
@IBInspectable var shadowRippleEnable: Bool = true

let rippleView = UIView()
let rippleBackgroundView = UIView()
var tempShadowRadius: CGFloat = 0
var tempShadowOpacity: CGFloat = 0

init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
setup()
}

init(frame: CGRect) {
super.init(frame: frame)
setup()
}

func setup() {

var rippleWidthPercent: CGFloat = 0.9
var size: CGFloat = CGRectGetWidth(bounds) * rippleWidthPercent
var x: CGFloat = (CGRectGetWidth(bounds)/2) - (size/2)
var y: CGFloat = (CGRectGetHeight(bounds)/2) - (size/2)
var corner: CGFloat = size/2

rippleView.backgroundColor = rippleColor
rippleView.frame = CGRectMake(x, y, size, size)
rippleView.layer.cornerRadius = corner

rippleBackgroundView.backgroundColor = rippleBackgroundColor
rippleBackgroundView.frame = bounds
rippleBackgroundView.alpha = 0

rippleOverBounds = false

layer.addSublayer(rippleBackgroundView.layer)
rippleBackgroundView.layer.addSublayer(rippleView.layer)

layer.shadowRadius = 0
layer.shadowOffset = CGSize(width: 0, height: 1)
layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).CGColor

}

override func beginTrackingWithTouch(touch: UITouch!, withEvent event: UIEvent!) -> Bool {
UIView.animateWithDuration(0.1, animations: {
self.rippleBackgroundView.alpha = 1
}, completion: nil)

self.rippleView.transform = CGAffineTransformMakeScale(0.5, 0.5)
UIView.animateWithDuration(0.7, delay: 0, options: .CurveEaseOut, animations: {
self.rippleView.transform = CGAffineTransformIdentity
}, completion: nil)

if shadowRippleEnable {
tempShadowRadius = layer.shadowRadius
tempShadowOpacity = layer.shadowOpacity

var shadowAnim = CABasicAnimation(keyPath:"shadowRadius")
shadowAnim.toValue = NSNumber.numberWithFloat(shadowRippleRadius)

var opacityAnim = CABasicAnimation(keyPath:"shadowOpacity")
opacityAnim.toValue = NSNumber.numberWithFloat(1)

var groupAnim = CAAnimationGroup()
groupAnim.duration = 0.7
groupAnim.fillMode = kCAFillModeForwards
groupAnim.removedOnCompletion = false
groupAnim.animations = [shadowAnim, opacityAnim]

layer.addAnimation(groupAnim, forKey:"shadow")
}


return super.beginTrackingWithTouch(touch, withEvent: event)
}

override func endTrackingWithTouch(touch: UITouch!, withEvent event: UIEvent!) {
super.endTrackingWithTouch(touch, withEvent: event)

UIView.animateWithDuration(0.1, animations: {
self.rippleBackgroundView.alpha = 1
}, completion: {(success: Bool) -> () in
UIView.animateWithDuration(0.6 , animations: {
self.rippleBackgroundView.alpha = 0
}, completion: {(success: Bool) -> () in

})
})

UIView.animateWithDuration(0.7, delay: 0, options: .CurveEaseOut | .BeginFromCurrentState, animations: {
self.rippleView.transform = CGAffineTransformIdentity

var shadowAnim = CABasicAnimation(keyPath:"shadowRadius")
shadowAnim.toValue = NSNumber.numberWithFloat(self.tempShadowRadius)

var opacityAnim = CABasicAnimation(keyPath:"shadowOpacity")
opacityAnim.toValue = NSNumber.numberWithFloat(self.tempShadowOpacity)

var groupAnim = CAAnimationGroup()
groupAnim.duration = 0.7
groupAnim.fillMode = kCAFillModeForwards
groupAnim.removedOnCompletion = false
groupAnim.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
groupAnim.animations = [shadowAnim, opacityAnim]

self.layer.addAnimation(groupAnim, forKey:"shadowBack")
}, completion: nil)


}

}

0 comments on commit 4a9373f

Please sign in to comment.