Skip to content

Open menu from right to left for hebrew and arabic. Pan gesture to close menu. #118

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
wants to merge 4 commits 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
68 changes: 52 additions & 16 deletions Sources/MenuAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ private extension MenuInteractiveTransition {

toViewController.view.transform = CGAffineTransform(scaleX: options.contentScale, y: options.contentScale)
addShadow(to: toViewController.view)

let newOrigin = CGPoint(x: screenWidth - options.visibleContentWidth, y: toViewController.view.frame.origin.y)

let isLeft = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? 1 : -1

let newOrigin = CGPoint(x: CGFloat(isLeft) * screenWidth + options.visibleContentWidth, y: toViewController.view.frame.origin.y)
let rect = CGRect(origin: newOrigin, size: toViewController.view.frame.size)

toViewController.view.frame = rect
Expand Down Expand Up @@ -214,7 +216,12 @@ private extension MenuInteractiveTransition {
func addShadow(to view: UIView) {
view.layer.shadowColor = currentItemOptions.shadow.color?.cgColor
view.layer.shadowOpacity = Float(currentItemOptions.shadow.opacity)
view.layer.shadowOffset = currentItemOptions.shadow.offset
let isLeft = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? true : false
if (isLeft) {
view.layer.shadowOffset = currentItemOptions.shadow.offset
} else {
view.layer.shadowOffset = currentItemOptions.shadow.offset_reverse
}
view.layer.shadowRadius = currentItemOptions.shadow.radius
view.layer.shadowPath = UIBezierPath(roundedRect: view.bounds, cornerRadius: currentItemOptions.cornerRadius).cgPath
view.layer.shouldRasterize = true
Expand Down Expand Up @@ -284,10 +291,16 @@ private extension MenuInteractiveTransition {
let dx = translation.x / recognizerView.bounds.width
let progress: CGFloat = abs(dx)
var velocity = recognizer.velocity(in: recognizerView).x

if !present {
velocity = -velocity
}

let isLeft = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? true : false

if (!isLeft) {
velocity = -velocity
}

switch recognizer.state {
case .began:
Expand All @@ -303,25 +316,46 @@ private extension MenuInteractiveTransition {
}

case .changed:
if transitionStarted && (present && dx > 0 || !present && dx < 0) {
guard let transitionContext = transitionContext else {
fatalError("Invalid `transitionContext` value. This property should not be nil")
if (!isLeft) {
if transitionStarted && (present && dx < 0 || !present && dx > 0) {
guard let transitionContext = transitionContext else {
fatalError("Invalid `transitionContext` value. This property should not be nil")
}
updateTransition(percentComplete: progress)
transitionContext.updateInteractiveTransition(progress)
}
} else {
if transitionStarted && (present && dx > 0 || !present && dx < 0) {
guard let transitionContext = transitionContext else {
fatalError("Invalid `transitionContext` value. This property should not be nil")
}
updateTransition(percentComplete: progress)
transitionContext.updateInteractiveTransition(progress)
}
updateTransition(percentComplete: progress)
transitionContext.updateInteractiveTransition(progress)
}

case .cancelled, .ended:
if transitionStarted {
guard let transitionContext = transitionContext else {
fatalError("Invalid `transitionContext` value. This property should not be nil")
}
if progress > 0.4, velocity >= 0 || progress > 0.01, velocity > 100 {
finishTransition(currentPercentComplete: progress)
transitionContext.finishInteractiveTransition()

if (!isLeft) {
if progress < 0.2 && velocity <= 0 {
cancelTransition(currentPercentComplete: progress)
transitionContext.cancelInteractiveTransition()
} else {
finishTransition(currentPercentComplete: progress)
transitionContext.finishInteractiveTransition()
}
} else {
cancelTransition(currentPercentComplete: progress)
transitionContext.cancelInteractiveTransition()
if progress > 0.2 && velocity >= 0 {
finishTransition(currentPercentComplete: progress)
transitionContext.finishInteractiveTransition()
} else {
cancelTransition(currentPercentComplete: progress)
transitionContext.cancelInteractiveTransition()
}
}

} else if transitionShouldStarted, !transitionStarted {
Expand Down Expand Up @@ -358,14 +392,16 @@ private extension MenuInteractiveTransition {
guard let contentSnapshotView = self.contentSnapshotView else {
fatalError("Invalid `contentSnapshotView` value. This property should not be nil")
}

let isLeft = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? 1 : -1

if present {
let newScale = 1 - (1 - options.contentScale) * percentComplete
let newX = totalWidth * percentComplete

contentSnapshotView.transform = CGAffineTransform(scaleX: newScale, y: newScale)

let newOrigin = CGPoint(x: newX, y: contentSnapshotView.frame.origin.y)
let newOrigin = CGPoint(x: CGFloat(isLeft) * newX, y: contentSnapshotView.frame.origin.y)
let rect = CGRect(origin: newOrigin, size: contentSnapshotView.frame.size)

contentSnapshotView.frame = rect
Expand All @@ -380,7 +416,7 @@ private extension MenuInteractiveTransition {
let newScale = options.contentScale + (1 - options.contentScale) * percentComplete
toViewController.view.transform = CGAffineTransform(scaleX: newScale, y: newScale)

let newOrigin = CGPoint(x: newX, y: toViewController.view.frame.origin.y)
let newOrigin = CGPoint(x: CGFloat(isLeft) * newX, y: toViewController.view.frame.origin.y)
let rect = CGRect(origin: newOrigin, size: toViewController.view.frame.size)

toViewController.view.frame = rect
Expand Down
1 change: 1 addition & 0 deletions Sources/SideMenuItemOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public struct SideMenuItemOptions {
public var color: UIColor? = UIColor.black
public var opacity: CGFloat = 0.3
public var offset: CGSize = CGSize(width: -5, height: 5)
public var offset_reverse: CGSize = CGSize(width: 5, height: 5)
public var radius: CGFloat = 3

public init() { }
Expand Down