diff --git a/TVButton/TVButton.swift b/TVButton/TVButton.swift index 79c115e..699a68c 100644 --- a/TVButton/TVButton.swift +++ b/TVButton/TVButton.swift @@ -3,6 +3,7 @@ // TVButton // // Created by Roy Marmelstein on 08/11/2015. +// Updated by Rodrigo Paschoaletto on 01/03/2019 // Copyright © 2015 Roy Marmelstein. All rights reserved. // @@ -30,18 +31,18 @@ public extension TVButtonLayer { TVButton Object */ open class TVButton: UIButton, UIGestureRecognizerDelegate { - + // MARK: Internal variables internal var containerView = UIView() internal var specularView = UIImageView() internal var tvButtonAnimation: TVButtonAnimation? - + internal var longPressGestureRecognizer: UILongPressGestureRecognizer? internal var panGestureRecognizer: UIPanGestureRecognizer? internal var tapGestureRecognizer: UITapGestureRecognizer? - + // MARK: Public variables - + /// Stack of TVButtonLayers inside the button open var layers: [TVButtonLayer]? { didSet { @@ -74,9 +75,9 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { self.layer.shadowColor = shadowColor.cgColor } } - + // MARK: Lifecycle - + /** Default init for TVObject with coder. */ @@ -84,7 +85,7 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { super.init(coder: aDecoder) setup() } - + /** Default init for TVObject with frame. */ @@ -92,7 +93,7 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { super.init(frame: frame) setup() } - + /** Lays out subviews. */ @@ -109,7 +110,7 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { return } } - + // Adjust size for every subview for subview in containerView.subviews { if subview == specularView { @@ -120,7 +121,7 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { } } } - + /** Button setup. Conducted on init. */ @@ -131,17 +132,17 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { containerView.layer.cornerRadius = cornerRadius self.clipsToBounds = true specularView.alpha = 0.0 - specularView.contentMode = UIViewContentMode.scaleAspectFill + specularView.contentMode = UIView.ContentMode.scaleAspectFill self.layer.shadowRadius = self.bounds.size.height/(2*shadowFactor) self.layer.shadowOffset = CGSize(width: 0.0, height: shadowFactor/3) self.layer.shadowOpacity = 0.5; tvButtonAnimation = TVButtonAnimation(button: self) self.addGestureRecognizers() } - - + + // MARK: UIGestureRecognizer actions and delegate - + /** Adds the gesture recognizers to the button. */ @@ -155,31 +156,31 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { longPressGestureRecognizer?.delegate = self self.addGestureRecognizer(longPressGestureRecognizer!) } - + /** Pan gesture recognizer handler. - Parameter gestureRecognizer: TVButton's UIPanGestureRecognizer. */ - func handlePan(_ gestureRecognizer: UIGestureRecognizer) { + @objc func handlePan(_ gestureRecognizer: UIGestureRecognizer) { self.gestureRecognizerDidUpdate(gestureRecognizer) } - + /** Long press gesture recognizer handler. - Parameter gestureRecognizer: TVButton's UILongPressGestureRecognizer. */ - func handleLongPress(_ gestureRecognizer: UIGestureRecognizer) { + @objc func handleLongPress(_ gestureRecognizer: UIGestureRecognizer) { self.gestureRecognizerDidUpdate(gestureRecognizer) } - + /** Tap gesture recognizer handler. Sends TouchUpInside to super. - Parameter gestureRecognizer: TVButton's UITapGestureRecognizer. */ - func handleTap(_ gestureRecognizer: UIGestureRecognizer) { - super.sendActions(for: UIControlEvents.touchUpInside) + @objc func handleTap(_ gestureRecognizer: UIGestureRecognizer) { + super.sendActions(for: UIControl.Event.touchUpInside) } - + /** Determines button's reaction to gesturerecognizer. - Parameter gestureRecognizer: either UITapGestureRecognizer or UILongPressGestureRecognizer. @@ -205,9 +206,9 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { } } } - + // MARK: UIGestureRecognizerDelegate - + /** UIGestureRecognizerDelegate function to allow two UIGestureRecognizers to be recognized simultaneously. - Parameter gestureRecognizer: First gestureRecognizer. @@ -216,5 +217,5 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate { open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { return true } - + } diff --git a/TVButton/TVButtonAnimation.swift b/TVButton/TVButtonAnimation.swift index 566b241..6a0d6d8 100644 --- a/TVButton/TVButtonAnimation.swift +++ b/TVButton/TVButtonAnimation.swift @@ -3,6 +3,7 @@ // TVButton // // Created by Roy Marmelstein on 10/11/2015. +// Updated by Rodrigo Paschoaletto on 01/03/2019 // Copyright © 2015 Roy Marmelstein. All rights reserved. // @@ -12,10 +13,10 @@ import Foundation TVButtonAnimation class */ internal class TVButtonAnimation { - + var highlightMode: Bool = false { didSet { - + } } var button: TVButton? @@ -23,7 +24,7 @@ internal class TVButtonAnimation { init(button: TVButton) { self.button = button } - + // Movement begins func enterMovement() { if highlightMode == true { @@ -41,21 +42,21 @@ internal class TVButtonAnimation { shaowOffsetAnimation.toValue = NSValue(cgSize: targetShadowOffset) shaowOffsetAnimation.duration = animationDuration shaowOffsetAnimation.isRemovedOnCompletion = false - shaowOffsetAnimation.fillMode = kCAFillModeForwards - shaowOffsetAnimation.timingFunction = CAMediaTimingFunction(name: "easeOut") + shaowOffsetAnimation.fillMode = CAMediaTimingFillMode.forwards + shaowOffsetAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName(rawValue: "easeOut")) tvButton.layer.add(shaowOffsetAnimation, forKey: "shadowOffset") CATransaction.commit() let shadowOpacityAnimation = CABasicAnimation(keyPath: "shadowOpacity") shadowOpacityAnimation.toValue = 0.6 shadowOpacityAnimation.duration = animationDuration shadowOpacityAnimation.isRemovedOnCompletion = false - shadowOpacityAnimation.fillMode = kCAFillModeForwards - shadowOpacityAnimation.timingFunction = CAMediaTimingFunction(name: "easeOut") + shadowOpacityAnimation.fillMode = CAMediaTimingFillMode.forwards + shadowOpacityAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName(rawValue: "easeOut")) tvButton.layer.add(shadowOpacityAnimation, forKey: "shadowOpacityAnimation") CATransaction.commit() } } - + // Movement continues func processMovement(_ point: CGPoint){ if (highlightMode == false) { @@ -69,22 +70,22 @@ internal class TVButtonAnimation { let xRotation = (dy - offsetY)*(rotateXFactor/tvButton.bounds.size.width) let yRotation = (offsetX - dx)*(rotateYFactor/tvButton.bounds.size.width) let zRotation = (xRotation + yRotation)/rotateZFactor - + let xTranslation = (-2*point.x/tvButton.bounds.size.width)*maxTranslationX let yTranslation = (-2*point.y/tvButton.bounds.size.height)*maxTranslationY - + let xRotateTransform = CATransform3DMakeRotation(degreesToRadians(xRotation), 1, 0, 0) let yRotateTransform = CATransform3DMakeRotation(degreesToRadians(yRotation), 0, 1, 0) let zRotateTransform = CATransform3DMakeRotation(degreesToRadians(zRotation), 0, 0, 1) - + let combinedRotateTransformXY = CATransform3DConcat(xRotateTransform, yRotateTransform) let combinedRotateTransformZ = CATransform3DConcat(combinedRotateTransformXY, zRotateTransform) let translationTransform = CATransform3DMakeTranslation(-xTranslation, yTranslation, 0.0) let combinedRotateTranslateTransform = CATransform3DConcat(combinedRotateTransformZ, translationTransform) let targetScaleTransform = CATransform3DMakeScale(highlightedScale, highlightedScale, highlightedScale) let combinedTransform = CATransform3DConcat(combinedRotateTranslateTransform, targetScaleTransform) - - UIView.animate(withDuration: animationDuration, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: { () -> Void in + + UIView.animate(withDuration: animationDuration, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: { () -> Void in tvButton.layer.transform = combinedTransform tvButton.specularView.alpha = specularAlpha tvButton.specularView.center = point @@ -93,13 +94,13 @@ internal class TVButtonAnimation { let scale = 1 + maxScaleDelta*CGFloat(adjusted/tvButton.containerView.subviews.count) let subview = tvButton.containerView.subviews[i] if subview != tvButton.specularView { - subview.contentMode = UIViewContentMode.redraw + subview.contentMode = UIView.ContentMode.redraw subview.frame.size = CGSize(width: tvButton.bounds.size.width*scale, height: tvButton.bounds.size.height*scale) } } }, completion: nil) - UIView.animate(withDuration: 0.16, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: { () -> Void in + UIView.animate(withDuration: 0.16, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: { () -> Void in for i in 1 ..< tvButton.containerView.subviews.count { let subview = tvButton.containerView.subviews[i] let xParallax = tvButton.parallaxIntensity*parallaxIntensityXFactor @@ -112,7 +113,7 @@ internal class TVButtonAnimation { } } - + // Movement ends func exitMovement() { if highlightMode == false { @@ -131,19 +132,19 @@ internal class TVButtonAnimation { let shaowOffsetAnimation = CABasicAnimation(keyPath: "shadowOffset") shaowOffsetAnimation.toValue = NSValue(cgSize: targetShadowOffset) shaowOffsetAnimation.duration = animationDuration - shaowOffsetAnimation.fillMode = kCAFillModeForwards + shaowOffsetAnimation.fillMode = CAMediaTimingFillMode.forwards shaowOffsetAnimation.isRemovedOnCompletion = false - shaowOffsetAnimation.timingFunction = CAMediaTimingFunction(name: "easeOut") + shaowOffsetAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName(rawValue: "easeOut")) tvButton.layer.add(shaowOffsetAnimation, forKey: "shadowOffset") let scaleAnimation = CABasicAnimation(keyPath: "transform") scaleAnimation.toValue = NSValue(caTransform3D: targetScaleTransform) scaleAnimation.duration = animationDuration scaleAnimation.isRemovedOnCompletion = false - scaleAnimation.fillMode = kCAFillModeForwards - scaleAnimation.timingFunction = CAMediaTimingFunction(name: "easeOut") + scaleAnimation.fillMode = CAMediaTimingFillMode.forwards + scaleAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName(rawValue: "easeOut")) tvButton.layer.add(scaleAnimation, forKey: "scaleAnimation") CATransaction.commit() - UIView.animate(withDuration: animationDuration, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: { () -> Void in + UIView.animate(withDuration: animationDuration, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: { () -> Void in tvButton.transform = CGAffineTransform.identity tvButton.specularView.alpha = 0.0 for i in 0 ..< tvButton.containerView.subviews.count { @@ -154,11 +155,11 @@ internal class TVButtonAnimation { }, completion:nil) } } - + // MARK: Convenience - + func degreesToRadians(_ value:CGFloat) -> CGFloat { - return value * CGFloat(M_PI) / 180.0 + return value * CGFloat(Double.pi) / 180.0 } }