-
Notifications
You must be signed in to change notification settings - Fork 227
Make rotating of cards customizable #76
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
Open
ilyavelilyaev
wants to merge
5
commits into
zhxnlai:master
Choose a base branch
from
ilyavelilyaev:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Pod::Spec.new do |s| | ||
s.name = "ZLSwipeableViewSwift" | ||
s.version = "0.0.7" | ||
s.version = "0.0.8" | ||
s.summary = "A simple view for building card like interface like Tinder and Potluck." | ||
s.description = <<-DESC | ||
ZLSwipeableViewSwift is a simple view for building card like interface like Tinder and Potluck. | ||
|
@@ -11,9 +11,9 @@ Pod::Spec.new do |s| | |
s.author = { "Zhixuan Lai" => "[email protected]" } | ||
s.social_media_url = "http://twitter.com/ZhixuanLai" | ||
|
||
s.platform = :ios, "8.0" | ||
s.platform = :ios, "9.0" | ||
|
||
s.source = { :git => "https://github.com/zhxnlai/ZLSwipeableViewSwift.git", :tag => "0.0.7" } | ||
s.source = { :git => "https://github.com/zhxnlai/ZLSwipeableViewSwift.git", :tag => "0.0.8" } | ||
s.source_files = "ZLSwipeableViewSwift/*.swift" | ||
|
||
s.framework = "UIKit" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,26 +13,26 @@ class ViewManager : NSObject { | |
// Snapping -> [Moving]+ -> Snapping | ||
// Snapping -> [Moving]+ -> Swiping -> Snapping | ||
enum State { | ||
case Snapping(CGPoint), Moving(CGPoint), Swiping(CGPoint, CGVector) | ||
case snapping(CGPoint), moving(CGPoint), swiping(CGPoint, CGVector) | ||
} | ||
|
||
var state: State { | ||
didSet { | ||
if case .Snapping(_) = oldValue, case let .Moving(point) = state { | ||
if case .snapping(_) = oldValue, case let .moving(point) = state { | ||
unsnapView() | ||
attachView(toPoint: point) | ||
} else if case .Snapping(_) = oldValue, case let .Swiping(origin, direction) = state { | ||
} else if case .snapping(_) = oldValue, case let .swiping(origin, direction) = state { | ||
unsnapView() | ||
attachView(toPoint: origin) | ||
pushView(fromPoint: origin, inDirection: direction) | ||
} else if case .Moving(_) = oldValue, case let .Moving(point) = state { | ||
} else if case .moving(_) = oldValue, case let .moving(point) = state { | ||
moveView(toPoint: point) | ||
} else if case .Moving(_) = oldValue, case let .Snapping(point) = state { | ||
} else if case .moving(_) = oldValue, case let .snapping(point) = state { | ||
detachView() | ||
snapView(point) | ||
} else if case .Moving(_) = oldValue, case let .Swiping(origin, direction) = state { | ||
} else if case .moving(_) = oldValue, case let .swiping(origin, direction) = state { | ||
pushView(fromPoint: origin, inDirection: direction) | ||
} else if case .Swiping(_, _) = oldValue, case let .Snapping(point) = state { | ||
} else if case .swiping(_, _) = oldValue, case let .snapping(point) = state { | ||
unpushView() | ||
detachView() | ||
snapView(point) | ||
|
@@ -41,46 +41,49 @@ class ViewManager : NSObject { | |
} | ||
|
||
/// To be added to view and removed | ||
private class ZLPanGestureRecognizer: UIPanGestureRecognizer { } | ||
private class ZLTapGestureRecognizer: UITapGestureRecognizer { } | ||
fileprivate class ZLPanGestureRecognizer: UIPanGestureRecognizer { } | ||
fileprivate class ZLTapGestureRecognizer: UITapGestureRecognizer { } | ||
|
||
static private let anchorViewWidth = CGFloat(1000) | ||
private var anchorView = UIView(frame: CGRect(x: 0, y: 0, width: anchorViewWidth, height: anchorViewWidth)) | ||
static fileprivate let anchorViewWidth = CGFloat(1000) | ||
fileprivate var anchorView = UIView(frame: CGRect(x: 0, y: 0, width: anchorViewWidth, height: anchorViewWidth)) | ||
|
||
private var snapBehavior: UISnapBehavior! | ||
private var viewToAnchorViewAttachmentBehavior: UIAttachmentBehavior! | ||
private var anchorViewToPointAttachmentBehavior: UIAttachmentBehavior! | ||
private var pushBehavior: UIPushBehavior! | ||
|
||
private let view: UIView | ||
private let containerView: UIView | ||
private let miscContainerView: UIView | ||
private let animator: UIDynamicAnimator | ||
private weak var swipeableView: ZLSwipeableView? | ||
|
||
init(view: UIView, containerView: UIView, index: Int, miscContainerView: UIView, animator: UIDynamicAnimator, swipeableView: ZLSwipeableView) { | ||
fileprivate var snapBehavior: UISnapBehavior! | ||
fileprivate var viewToAnchorViewAttachmentBehavior: UIAttachmentBehavior! | ||
fileprivate var anchorViewToPointAttachmentBehavior: UIAttachmentBehavior! | ||
fileprivate var pushBehavior: UIPushBehavior! | ||
|
||
fileprivate let view: UIView | ||
fileprivate let containerView: UIView | ||
fileprivate let miscContainerView: UIView | ||
fileprivate let animator: UIDynamicAnimator | ||
fileprivate let rotatingSpeedMultiplyer: CGFloat | ||
|
||
fileprivate weak var swipeableView: ZLSwipeableView? | ||
|
||
init(view: UIView, containerView: UIView, index: Int, miscContainerView: UIView, animator: UIDynamicAnimator, swipeableView: ZLSwipeableView, rotatingSpeedMultiplyer rotMul: CGFloat) { | ||
self.view = view | ||
self.containerView = containerView | ||
self.miscContainerView = miscContainerView | ||
self.animator = animator | ||
self.swipeableView = swipeableView | ||
self.state = ViewManager.defaultSnappingState(view) | ||
self.rotatingSpeedMultiplyer = rotMul | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here as well, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and anywhere else in the file where you've used or defined a variable with that in it's name |
||
|
||
super.init() | ||
|
||
view.addGestureRecognizer(ZLPanGestureRecognizer(target: self, action: #selector(ViewManager.handlePan(_:)))) | ||
view.addGestureRecognizer(ZLTapGestureRecognizer(target: self, action: #selector(ViewManager.handleTap(_:)))) | ||
miscContainerView.addSubview(anchorView) | ||
containerView.insertSubview(view, atIndex: index) | ||
containerView.insertSubview(view, at: index) | ||
} | ||
|
||
static func defaultSnappingState(view: UIView) -> State { | ||
return .Snapping(view.convertPoint(view.center, fromView: view.superview)) | ||
static func defaultSnappingState(_ view: UIView) -> State { | ||
return .snapping(view.convert(view.center, from: view.superview)) | ||
} | ||
|
||
func snappingStateAtContainerCenter() -> State { | ||
guard let swipeableView = swipeableView else { return ViewManager.defaultSnappingState(view) } | ||
return .Snapping(containerView.convertPoint(swipeableView.center, fromView: swipeableView.superview)) | ||
return .snapping(containerView.convert(swipeableView.center, from: swipeableView.superview)) | ||
} | ||
|
||
deinit { | ||
|
@@ -98,7 +101,7 @@ class ViewManager : NSObject { | |
} | ||
|
||
for gestureRecognizer in view.gestureRecognizers! { | ||
if gestureRecognizer.isKindOfClass(ZLPanGestureRecognizer.classForCoder()) { | ||
if gestureRecognizer.isKind(of: ZLPanGestureRecognizer.classForCoder()) { | ||
view.removeGestureRecognizer(gestureRecognizer) | ||
} | ||
} | ||
|
@@ -107,108 +110,110 @@ class ViewManager : NSObject { | |
view.removeFromSuperview() | ||
} | ||
|
||
func handlePan(recognizer: UIPanGestureRecognizer) { | ||
func handlePan(_ recognizer: UIPanGestureRecognizer) { | ||
guard let swipeableView = swipeableView else { return } | ||
|
||
let translation = recognizer.translationInView(containerView) | ||
let location = recognizer.locationInView(containerView) | ||
let velocity = recognizer.velocityInView(containerView) | ||
let translation = recognizer.translation(in: containerView) | ||
let location = recognizer.location(in: containerView) | ||
let velocity = recognizer.velocity(in: containerView) | ||
let movement = Movement(location: location, translation: translation, velocity: velocity) | ||
|
||
switch recognizer.state { | ||
case .Began: | ||
guard case .Snapping(_) = state else { return } | ||
state = .Moving(location) | ||
swipeableView.didStart?(view: view, atLocation: location) | ||
case .Changed: | ||
guard case .Moving(_) = state else { return } | ||
state = .Moving(location) | ||
swipeableView.swiping?(view: view, atLocation: location, translation: translation) | ||
case .Ended, .Cancelled: | ||
guard case .Moving(_) = state else { return } | ||
if swipeableView.shouldSwipeView(view: view, movement: movement, swipeableView: swipeableView) { | ||
case .began: | ||
guard case .snapping(_) = state else { return } | ||
state = .moving(location) | ||
swipeableView.didStart?(view, location) | ||
case .changed: | ||
guard case .moving(_) = state else { return } | ||
state = .moving(location) | ||
swipeableView.swiping?(view, location, translation) | ||
case .ended, .cancelled: | ||
guard case .moving(_) = state else { return } | ||
if swipeableView.shouldSwipeView(view, movement, swipeableView) { | ||
let directionVector = CGVector(point: translation.normalized * max(velocity.magnitude, swipeableView.minVelocityInPointPerSecond)) | ||
state = .Swiping(location, directionVector) | ||
state = .swiping(location, directionVector) | ||
swipeableView.swipeView(view, location: location, directionVector: directionVector) | ||
} else { | ||
state = snappingStateAtContainerCenter() | ||
swipeableView.didCancel?(view: view) | ||
swipeableView.didCancel?(view) | ||
} | ||
swipeableView.didEnd?(view: view, atLocation: location) | ||
swipeableView.didEnd?(view, location) | ||
default: | ||
break | ||
} | ||
} | ||
|
||
func handleTap(recognizer: UITapGestureRecognizer) { | ||
guard let swipeableView = swipeableView, topView = swipeableView.topView() else { return } | ||
func handleTap(_ recognizer: UITapGestureRecognizer) { | ||
guard let swipeableView = swipeableView, let topView = swipeableView.topView() else { return } | ||
|
||
let location = recognizer.locationInView(containerView) | ||
swipeableView.didTap?(view: topView, atLocation: location) | ||
let location = recognizer.location(in: containerView) | ||
swipeableView.didTap?(topView, location) | ||
} | ||
|
||
private func snapView(point: CGPoint) { | ||
snapBehavior = UISnapBehavior(item: view, snapToPoint: point) | ||
fileprivate func snapView(_ point: CGPoint) { | ||
snapBehavior = UISnapBehavior(item: view, snapTo: point) | ||
snapBehavior!.damping = 0.75 | ||
addBehavior(snapBehavior) | ||
} | ||
|
||
private func unsnapView() { | ||
fileprivate func unsnapView() { | ||
guard let snapBehavior = snapBehavior else { return } | ||
removeBehavior(snapBehavior) | ||
} | ||
|
||
private func attachView(toPoint point: CGPoint) { | ||
fileprivate func attachView(toPoint point: CGPoint) { | ||
anchorView.center = point | ||
anchorView.backgroundColor = UIColor.blueColor() | ||
anchorView.hidden = true | ||
|
||
|
||
// attach aView to anchorView | ||
let p = view.center | ||
viewToAnchorViewAttachmentBehavior = UIAttachmentBehavior(item: view, offsetFromCenter: UIOffset(horizontal: -(p.x - point.x), vertical: -(p.y - point.y)), attachedToItem: anchorView, offsetFromCenter: UIOffsetZero) | ||
viewToAnchorViewAttachmentBehavior!.length = 0 | ||
let x = (point.x - p.x) * rotatingSpeedMultiplyer + p.x | ||
let y = (point.y - p.y) * rotatingSpeedMultiplyer + p.y | ||
let newPoint = CGPoint(x: x, y: y) | ||
|
||
viewToAnchorViewAttachmentBehavior = UIAttachmentBehavior.pinAttachment(with: view, attachedTo: anchorView, attachmentAnchor: newPoint) | ||
|
||
|
||
// attach anchorView to point | ||
anchorViewToPointAttachmentBehavior = UIAttachmentBehavior(item: anchorView, offsetFromCenter: UIOffsetZero, attachedToAnchor: point) | ||
anchorViewToPointAttachmentBehavior = UIAttachmentBehavior(item: anchorView, offsetFromCenter: UIOffset.zero, attachedToAnchor: point) | ||
anchorViewToPointAttachmentBehavior!.damping = 100 | ||
anchorViewToPointAttachmentBehavior!.length = 0 | ||
|
||
addBehavior(viewToAnchorViewAttachmentBehavior!) | ||
addBehavior(anchorViewToPointAttachmentBehavior!) | ||
} | ||
|
||
private func moveView(toPoint point: CGPoint) { | ||
fileprivate func moveView(toPoint point: CGPoint) { | ||
guard let _ = viewToAnchorViewAttachmentBehavior, let toPoint = anchorViewToPointAttachmentBehavior else { return } | ||
toPoint.anchorPoint = point | ||
} | ||
|
||
private func detachView() { | ||
fileprivate func detachView() { | ||
guard let viewToAnchorViewAttachmentBehavior = viewToAnchorViewAttachmentBehavior, let anchorViewToPointAttachmentBehavior = anchorViewToPointAttachmentBehavior else { return } | ||
removeBehavior(viewToAnchorViewAttachmentBehavior) | ||
removeBehavior(anchorViewToPointAttachmentBehavior) | ||
} | ||
|
||
private func pushView(fromPoint point: CGPoint, inDirection direction: CGVector) { | ||
fileprivate func pushView(fromPoint point: CGPoint, inDirection direction: CGVector) { | ||
guard let _ = viewToAnchorViewAttachmentBehavior, let anchorViewToPointAttachmentBehavior = anchorViewToPointAttachmentBehavior else { return } | ||
|
||
removeBehavior(anchorViewToPointAttachmentBehavior) | ||
|
||
pushBehavior = UIPushBehavior(items: [anchorView], mode: .Instantaneous) | ||
pushBehavior = UIPushBehavior(items: [anchorView], mode: .instantaneous) | ||
pushBehavior.pushDirection = direction | ||
addBehavior(pushBehavior) | ||
} | ||
|
||
private func unpushView() { | ||
fileprivate func unpushView() { | ||
guard let pushBehavior = pushBehavior else { return } | ||
removeBehavior(pushBehavior) | ||
} | ||
|
||
private func addBehavior(behavior: UIDynamicBehavior) { | ||
fileprivate func addBehavior(_ behavior: UIDynamicBehavior) { | ||
animator.addBehavior(behavior) | ||
} | ||
|
||
private func removeBehavior(behavior: UIDynamicBehavior) { | ||
fileprivate func removeBehavior(_ behavior: UIDynamicBehavior) { | ||
animator.removeBehavior(behavior) | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason this change to drop support for iOS 8?