Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension of UIView #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Extension of UIView
MKView - an extension of UIView with material kit effect.
arpwal committed Jan 3, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit d7ec50ba92a533bd173b4e11ed488ff9efdeda12
120 changes: 120 additions & 0 deletions Source/MKView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//
// MKView.swift
// Ameri
//
// Created by Arpit Agarwal on 1/3/16.
// Copyright © 2016 Ameri International, Inc. All rights reserved.
//

import UIKit

@IBDesignable
public class MKView : UIView
{
@IBInspectable public var maskEnabled: Bool = true {
didSet {
mkLayer.enableMask(maskEnabled)
}
}
@IBInspectable public var rippleLocation: MKRippleLocation = .TapLocation {
didSet {
mkLayer.rippleLocation = rippleLocation
}
}
@IBInspectable public var ripplePercent: Float = 0.9 {
didSet {
mkLayer.ripplePercent = ripplePercent
}
}
@IBInspectable public var backgroundLayerCornerRadius: CGFloat = 0.0 {
didSet {
mkLayer.setBackgroundLayerCornerRadius(backgroundLayerCornerRadius)
}
}
// animations
@IBInspectable public var shadowAniEnabled: Bool = true
@IBInspectable public var backgroundAniEnabled: Bool = true {
didSet {
if !backgroundAniEnabled {
mkLayer.enableOnlyCircleLayer()
}
}
}
@IBInspectable public var rippleAniDuration: Float = 0.75
@IBInspectable public var backgroundAniDuration: Float = 1.0
@IBInspectable public var shadowAniDuration: Float = 0.65

@IBInspectable public var rippleAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var backgroundAniTimingFunction: MKTimingFunction = .Linear
@IBInspectable public var shadowAniTimingFunction: MKTimingFunction = .EaseOut

@IBInspectable public var cornerRadius: CGFloat = 2.5 {
didSet {
layer.cornerRadius = cornerRadius
mkLayer.setMaskLayerCornerRadius(cornerRadius)
}
}
// color
@IBInspectable public var rippleLayerColor: UIColor = UIColor(white: 0.45, alpha: 0.5) {
didSet {
mkLayer.setCircleLayerColor(rippleLayerColor)
}
}
@IBInspectable public var backgroundLayerColor: UIColor = UIColor(white: 0.75, alpha: 0.25) {
didSet {
mkLayer.setBackgroundLayerColor(backgroundLayerColor)
}
}
override public var bounds: CGRect {
didSet {
mkLayer.superLayerDidResize()
}
}

private lazy var mkLayer: MKLayer = MKLayer(superLayer: self.layer)

// MARK - initilization
override public init(frame: CGRect) {
super.init(frame: frame)
setupLayer()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupLayer()
}

// MARK - setup methods
private func setupLayer() {
cornerRadius = 2.5
mkLayer.setBackgroundLayerColor(backgroundLayerColor)
mkLayer.setCircleLayerColor(rippleLayerColor)
}

// MARK - location tracking methods
public override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let tapLocation = touches.first?.locationInView(self) {

if rippleLocation == .TapLocation {
mkLayer.didChangeTapLocation(tapLocation)
}

// rippleLayer animation
mkLayer.animateScaleForCircleLayer(0.45, toScale: 1.0, timingFunction: rippleAniTimingFunction, duration: CFTimeInterval(self.rippleAniDuration))

// backgroundLayer animation
if backgroundAniEnabled {
mkLayer.animateAlphaForBackgroundLayer(backgroundAniTimingFunction, duration: CFTimeInterval(self.backgroundAniDuration))
}

// shadow animation for self
if shadowAniEnabled {
let shadowRadius = layer.shadowRadius
let shadowOpacity = layer.shadowOpacity
let duration = CFTimeInterval(shadowAniDuration)
mkLayer.animateSuperLayerShadow(10, toRadius: shadowRadius, fromOpacity: 0, toOpacity: shadowOpacity, timingFunction: shadowAniTimingFunction, duration: duration)
}
}
super.touchesBegan(touches, withEvent: event)
}
}