From d7ec50ba92a533bd173b4e11ed488ff9efdeda12 Mon Sep 17 00:00:00 2001 From: asquared Date: Sun, 3 Jan 2016 10:09:30 -0800 Subject: [PATCH] Extension of UIView MKView - an extension of UIView with material kit effect. --- Source/MKView.swift | 120 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 Source/MKView.swift diff --git a/Source/MKView.swift b/Source/MKView.swift new file mode 100644 index 0000000..4bf786e --- /dev/null +++ b/Source/MKView.swift @@ -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, 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) + } +}