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

Fixes for Swift 4 #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
51 changes: 26 additions & 25 deletions TVButton/TVButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -74,25 +75,25 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate {
self.layer.shadowColor = shadowColor.cgColor
}
}

// MARK: Lifecycle

/**
Default init for TVObject with coder.
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

/**
Default init for TVObject with frame.
*/
override public init(frame: CGRect) {
super.init(frame: frame)
setup()
}

/**
Lays out subviews.
*/
Expand All @@ -109,7 +110,7 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate {
return
}
}

// Adjust size for every subview
for subview in containerView.subviews {
if subview == specularView {
Expand All @@ -120,7 +121,7 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate {
}
}
}

/**
Button setup. Conducted on init.
*/
Expand All @@ -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.
*/
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -216,5 +217,5 @@ open class TVButton: UIButton, UIGestureRecognizerDelegate {
open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

}
49 changes: 25 additions & 24 deletions TVButton/TVButtonAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//

Expand All @@ -12,18 +13,18 @@ import Foundation
TVButtonAnimation class
*/
internal class TVButtonAnimation {

var highlightMode: Bool = false {
didSet {

}
}
var button: TVButton?

init(button: TVButton) {
self.button = button
}

// Movement begins
func enterMovement() {
if highlightMode == true {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -112,7 +113,7 @@ internal class TVButtonAnimation {

}
}

// Movement ends
func exitMovement() {
if highlightMode == false {
Expand All @@ -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 {
Expand All @@ -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
}

}