Skip to content

Commit

Permalink
Add Swift version to podspec file + format & lint the code
Browse files Browse the repository at this point in the history
Cocoapods 1.4.0 includes the ability to specify the Swift version in the podspec (previously this could be done by creating a separate file in the repo). This allows Cocoapods to know which target to set for the Pod - instead of going for a default value, which can cause troubles when mixing Swift 4 and 3 code.

This change adds the Swift version to the Podspec.

The other changes includes some basic formatting and linting (mostly removal of whitespace and following Swift styleguides).

Also updates the Podspec version.
  • Loading branch information
manucheri authored and Sundin committed Mar 12, 2018
1 parent c039915 commit 7d5df66
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 104 deletions.
3 changes: 2 additions & 1 deletion KSSwipeStack.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'KSSwipeStack'
s.version = '0.4.2'
s.version = '0.4.3'
s.summary = 'A Swift card swiping library created by Kicksort Consulting AB'

# This description is used to generate tags and improve search results.
Expand All @@ -34,6 +34,7 @@ DOCS: https://github.com/Kicksort/KSSwipeStack
s.source = { :git => 'https://github.com/kicksort/KSSwipeStack.git', :tag => s.version.to_s }

s.ios.deployment_target = '8.0'
s.swift_version = '4.0'

s.source_files = 'KSSwipeStack/Classes/**/*'

Expand Down
10 changes: 5 additions & 5 deletions KSSwipeStack/Classes/PanDirectionGestureRecognizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ public enum PanDirection {

public class PanDirectionGestureRecognizer: UIPanGestureRecognizer {
let direction: PanDirection

init(direction: PanDirection, target: AnyObject, action: Selector) {
self.direction = direction
super.init(target: target, action: action)
}
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {

public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
super.touchesMoved(touches, with: event)

if state == .began {
guard let view = self.view else {
return
}

let v = velocity(in: view)
switch direction {
case .Horizontal where fabs(v.y) > fabs(v.x):
Expand Down
2 changes: 1 addition & 1 deletion KSSwipeStack/Classes/SwipableData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/**
Protocol which must be implemented by all data models meant to be swiped in the card stack.
Protocol which must be implemented by all data models meant to be swiped in the card stack.
*/
public protocol SwipableData {
/**
Expand Down
26 changes: 14 additions & 12 deletions KSSwipeStack/Classes/SwipableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,59 @@ import UIKit
*/
open class SwipableView: UIView {
private var data: SwipableData?

public override init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {

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

/**
Is fired when the view is being moved within the stack.
Is fired when the view is being moved within the stack.
## You can here change the visuals of the view based on:
- Whether or not the card is being swiped to the right (liked) or (disliked)
- The requested opacity of any overlays the view might have, based on the x-position of the view.
- parameter like: true for like (right) and false for dislike (left)
- parameter opacity: the requested opacity of overlays. Based on x-position
*/
open func respondToSwipe(like: Bool, opacity: Float) {
}

/**
Should contain logic to reset the view to its initial, visual state.
Should contain logic to reset the view to its initial, visual state.
This is for example called when a card is released, and snaps back to the center of the view.
*/
open func resetView() {
}

/**
This is being fired when a view is 'dimissed' from the stack.
For example when it has been swiped away and left the view.
*/
open func respondToDismiss() {
}

/**
Set the data this SwipableView is a representation of.
- parameter data: The data it is meant to represent.
*/
open func setData(_ data: SwipableData) {
self.data = data
}

/**
Get the data this SwipableView is a representation of.
- returns: The data it is meant to represent.
*/
open func getData() -> SwipableData? {
return data
}

/**
Should return whether or not the user is to be allowed to undo swiping this card.
*/
Expand Down
2 changes: 1 addition & 1 deletion KSSwipeStack/Classes/SwipeDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public protocol SwipeDelegate {
- parameter swipe: The swipe which just occured.
*/
func onNext(_ swipe: Swipe)
}
}
2 changes: 1 addition & 1 deletion KSSwipeStack/Classes/SwipeDirection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum SwipeDirection {
case right
case up
case down

func getSwipeEndpoint() -> CGPoint {
switch self {
case .left:
Expand Down
55 changes: 28 additions & 27 deletions KSSwipeStack/Classes/SwipeHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class SwipeHelper {
private let swipeViewSize: CGSize
private let swipeViewCenter: CGPoint
open var options = SwipeOptions()

init(with frame: CGRect) {
swipeViewSize = frame.size
swipeViewCenter = CGPoint(x: frame.width / 2, y: frame.height / 2)
}

/// Move and animate a view to a desired position
///
/// - Parameters:
Expand All @@ -28,7 +28,7 @@ class SwipeHelper {
func move(_ card: UIView, duration: Double = 0.25, toPoint: CGPoint) {
move(card, duration: duration, toPoint: toPoint, completion: {})
}

/// Move and animate a view to a desired position
///
/// - Parameters:
Expand All @@ -44,20 +44,21 @@ class SwipeHelper {
completion()
})
}

/// Move and animate a view to a desired position,
/// transforming the view according to the current SwipeOptions
/// Uses dismissAnimationDuration set in SwipeOptions
/// - Parameters:
/// - card: The view to be move
/// - toPoint: Destination of the move action
/// - completion: Callback fireing when the animation is done and the view is moved.

func moveFastAndTransform(_ card: SwipableView, toPoint: CGPoint, completion: @escaping () -> Void) {
addBorderToCard(card)
let rotation = self.calculateRotationAnimation(cardCenter: toPoint)
let scale = self.calculateScaleAnimation(cardCenter: toPoint)

let rotation = calculateRotationAnimation(cardCenter: toPoint)
let scale = calculateScaleAnimation(cardCenter: toPoint)

card.respondToSwipe(like: toPoint.x > 0, opacity: toPoint.equalTo(swipeViewCenter) ? 0.0 : 1.0)
UIView.animate(withDuration: options.dismissAnimationDuration, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
card.center = toPoint
Expand All @@ -67,43 +68,43 @@ class SwipeHelper {
completion()
})
}

/// Calculate the magnitude if a throw based on the velocity vector
/// Used when determining if a card has been thrown 'hard enough' to be dismissed.
/// - Parameter velocity: velocity vector of the gesture
/// - Returns: magnitude of the throw
func calculateThrowMagnitude(for velocity: CGPoint ) -> Float {
func calculateThrowMagnitude(for velocity: CGPoint) -> Float {
let velXSq = Float(velocity.x) * Float(velocity.x)
let velYSq = Float(velocity.y) * Float(velocity.y)
return sqrtf(velXSq + velYSq)
}

/// Returns a view to its original visual state in regards to border, rotation and scale.
/// Animates the reset of the view
/// - Parameter card: View to reset
func resetCard(_ card: UIView) {
let rotation = CATransform3DMakeRotation(0, 0, 0, 1)
let scale = CATransform3DMakeScale(1.0, 1.0, 1.0)

let borderAnim = CABasicAnimation(keyPath: "borderWidth")
borderAnim.fromValue = card.layer.borderWidth
borderAnim.toValue = 0
borderAnim.duration = 0.1
card.layer.borderWidth = 0

let cornerAnim = CABasicAnimation(keyPath: "cornerRadius")
cornerAnim.fromValue = card.layer.cornerRadius
cornerAnim.toValue = 0
cornerAnim.duration = 0.1
card.layer.cornerRadius = 0

let both = CAAnimationGroup()
both.duration = 0.1
both.animations = [cornerAnim, borderAnim]
both.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)

card.layer.add(both, forKey: "both")

UIView.animate(withDuration: 0.1) {
card.layer.transform = CATransform3DConcat(rotation, scale)
}
Expand All @@ -118,18 +119,18 @@ class SwipeHelper {
card.layer.transform = CATransform3DConcat(rotation, scale)
addBorderToCard(card)
}

func addBorderToCard(_ card: UIView) {
card.layer.borderWidth = 10
card.layer.borderColor = UIColor.white.cgColor
card.layer.cornerRadius = 10
card.layer.masksToBounds = true
}

private func calculateScaleAnimation(cardCenter: CGPoint) -> CATransform3D {
let horizontalDistance = calculateHorizontalDistanceFromCenter(cardCenter)
let verticalDistance = calculateVerticalDistanceFromCenter(cardCenter)

var scaleFactor = CGFloat(0)
if horizontalDistance >= verticalDistance {
scaleFactor = CGFloat(1 - horizontalDistance / 800.0)
Expand All @@ -138,45 +139,45 @@ class SwipeHelper {
}
return CATransform3DMakeScale(scaleFactor, scaleFactor, 1.0)
}

private func calculateRotationAnimation(cardCenter: CGPoint) -> CATransform3D {
let xFromCenter = Double(cardCenter.x - swipeViewSize.width / 2)
var rads = CGFloat(xFromCenter/10.0 * .pi / 180.0)
var rads = CGFloat(xFromCenter / 10.0 * .pi / 180.0)
if abs(rads) > 1.4 {
rads = -rads
}
return CATransform3DMakeRotation(rads, 0, 0, 1)
}

/// Calculates the distance in the horizontal plane from the position of a view to the center of the screen
///
/// - Parameter cardCenter: A positonal coordinate, preferably the center of a view.
/// - Returns: The horizontal distance from the center of the screen
private func calculateHorizontalDistanceFromCenter(_ cardCenter: CGPoint) -> Double {
return Double(abs(cardCenter.x - swipeViewSize.width / 2))
}

/// Calculates the distance in the vertical plane from the position of a view to the center of the screen
///
/// - Parameter cardCenter: A positonal coordinate, preferably the center of a view.
/// - Returns: The vertical distance from the center of the screen
private func calculateVerticalDistanceFromCenter(_ cardCenter: CGPoint) -> Double {
return Double(abs(cardCenter.y - swipeViewSize.height / 2))
}

/// Calculate a proper destination for a dismissal of a view based on its position
/// Places the view far to the left if the view is to the left the the center of the screen and vice versa.
/// - Parameter card: View which endpoint to calculate
/// - Returns: Proper destination for the view
func calculateEndpoint(_ card: UIView) -> CGPoint {
let deltaX = card.center.x - swipeViewSize.width / 2
let deltaY = card.center.y - swipeViewSize.height / 2

let k = deltaY / deltaX
let toX = deltaX < 0 ? -swipeViewSize.height / 2 : swipeViewSize.width + swipeViewSize.height / 2
return CGPoint(x: toX, y: toX * k)
}

/// Calculate a proper destination for a dismissal of a view based on current velocity
/// Places the view far to the left if the view is currently moving to the left and vice versa.
/// The angle from the center to the proposed destination of the view is based on the angle of the velocity vector
Expand All @@ -187,7 +188,7 @@ class SwipeHelper {
let toX = velocity.x < 0 ? -swipeViewSize.height / 2 : swipeViewSize.width + swipeViewSize.height / 2
return CGPoint(x: toX, y: toX * k)
}

/// Converts a position with coordinates with the origin of the screen as origo to one using the center of the screen as origo.
/// Can be used to convert a origin value to a center value refering to the same positioning of a full screen view.
/// - Parameter center: Position using origin as origo
Expand Down
4 changes: 2 additions & 2 deletions KSSwipeStack/Classes/SwipeOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public struct SwipeOptions {
public var refillThreshold = 10
public var dismissAnimationDuration = 0.25
public var freezeWhenDismissing = false
public init(){}

public init() {}
}
Loading

0 comments on commit 7d5df66

Please sign in to comment.