Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
Animation glitches
ScrollView's behaviour 
Unnecessary print statement
  • Loading branch information
PaoloCuscela committed Aug 4, 2018
1 parent 42f11af commit f3befb4
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 74 deletions.
8 changes: 6 additions & 2 deletions Cards/Sources/Card.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,17 @@ extension Card: UIGestureRecognizerDelegate {
cardTapped()
}

open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
resetAnimated()
}

override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

if let superview = self.superview {
originalFrame = superview.convert(self.frame, to: nil)
}
// Jie - Temporarily disable Push Back animation as it sometimes conflict with Auto Layout
// pushBackAnimated()

pushBackAnimated()
}
}

Expand Down
60 changes: 33 additions & 27 deletions Cards/Sources/DetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ internal class DetailViewController: UIViewController {
var blurView = UIVisualEffectView(effect: UIBlurEffect(style: .extraLight ))
weak var detailView: UIView?
var scrollView = UIScrollView()
var originalFrame = CGRect.zero
var snap = UIView()
weak var card: Card!
weak var delegate: CardDelegate?
var isFullscreen = false
var isFullscreen = false {
didSet { scrollViewOriginalYPosition = isFullscreen ? 0 : 40 }
}

fileprivate var xButton = XButton()
fileprivate var scrollPosition = CGFloat()
fileprivate var scrollViewOriginalYPosition = CGFloat()

override var prefersStatusBarHidden: Bool {
if isFullscreen { return true }
Expand Down Expand Up @@ -70,7 +73,7 @@ internal class DetailViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {

originalFrame = scrollView.frame
//originalFrame = scrollView.frame

if isFullscreen {
view.addSubview(xButton)
Expand All @@ -88,13 +91,10 @@ internal class DetailViewController: UIViewController {

scrollView.contentSize = CGSize(width: scrollView.bounds.width, height: detail.frame.maxY)


xButton.frame = CGRect (x: scrollView.frame.maxX - 20 - 40,
y: scrollView.frame.minY + 20,
width: 40,
height: 40)



}

Expand All @@ -118,20 +118,23 @@ internal class DetailViewController: UIViewController {

func layout(_ rect: CGRect, isPresenting: Bool, isAnimating: Bool = true, transform: CGAffineTransform = CGAffineTransform.identity){

// Layout for dismiss
guard isPresenting else {

scrollView.frame = rect.applying(transform)
scrollView.layer.cornerRadius = card.cardRadius
card.backgroundIV.frame = scrollView.bounds
card.layout(animating: isAnimating)
return
}

// Layout for present in fullscreen
if isFullscreen {

scrollView.layer.cornerRadius = 0
scrollView.frame = view.bounds
scrollView.frame.origin.y = 0
print(scrollView.frame)

// Layout for present in non-fullscreen
} else {
scrollView.frame.size = CGSize(width: LayoutHelper.XScreen(85), height: LayoutHelper.YScreen(100) - 20)
scrollView.center = blurView.center
Expand All @@ -142,9 +145,9 @@ internal class DetailViewController: UIViewController {

card.backgroundIV.frame.origin = scrollView.bounds.origin
card.backgroundIV.frame.size = CGSize( width: scrollView.bounds.width,
height: card.backgroundIV.bounds.height)
height: card.backgroundIV.bounds.height)
card.layout(animating: isAnimating)

}


Expand All @@ -164,41 +167,44 @@ extension DetailViewController: UIScrollViewDelegate {
public func scrollViewDidScroll(_ scrollView: UIScrollView) {

let y = scrollView.contentOffset.y
let origin = originalFrame.origin.y
let currentOrigin = originalFrame.origin.y

//xButton.alpha = y - (card.backgroundIV.bounds.height * 0.6)
let offset = scrollView.frame.origin.y - scrollViewOriginalYPosition

if (y<0 || currentOrigin > origin) {
// Behavior when scroll view is pulled down
if (y<0) {
scrollView.frame.origin.y -= y/2

scrollView.contentOffset.y = 0

// Behavior when scroll view is pulled down and then up
} else if ( offset > 0) {

scrollView.contentOffset.y = 0
scrollView.frame.origin.y -= y/2
}

guard offset < 60 else { dismissVC(); return }
card.delegate?.cardDetailIsScrolling?(card: card)
}

public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

let origin = originalFrame.origin.y
let currentOrigin = scrollView.frame.origin.y
let offset = scrollView.frame.origin.y - scrollViewOriginalYPosition

// Pull down speed calculations
let max = 4.0
let min = 2.0
var speed = Double(-velocity.y)

if speed > max { speed = max }
if speed < min { speed = min }

//self.bounceIntensity = CGFloat(speed-1)
speed = (max/speed*min)/10

guard (currentOrigin - origin) < 60 else { dismiss(animated: true, completion: nil); return }
UIView.animate(withDuration: speed) { scrollView.frame.origin.y = self.originalFrame.origin.y }
}

public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
guard offset < 60 else { dismissVC(); return }
guard offset > 0 else { return }

UIView.animate(withDuration: 0.1) { scrollView.frame.origin.y = self.originalFrame.origin.y }
// Come back after pull animation
UIView.animate(withDuration: speed, animations: {
scrollView.frame.origin.y = self.scrollViewOriginalYPosition
self.scrollView.contentOffset.y = 0
})
}

}
Expand Down
14 changes: 12 additions & 2 deletions Cards/Sources/LayoutHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,21 @@ open class LayoutHelper {
}

static open func XScreen(_ percentage: CGFloat) -> CGFloat {
return percentage * UIScreen.main.bounds.width / 100

if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
return percentage * UIScreen.main.bounds.height / 100
} else {
return percentage * UIScreen.main.bounds.width / 100
}
}

static open func YScreen(_ percentage: CGFloat) -> CGFloat {
return percentage * UIScreen.main.bounds.height / 100

if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
return percentage * UIScreen.main.bounds.width / 100
} else {
return percentage * UIScreen.main.bounds.height / 100
}
}

}
Expand Down
8 changes: 6 additions & 2 deletions Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
6D965BA21FA7974100705AAA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6D965BA01FA7974100705AAA /* Main.storyboard */; };
6D965BA41FA79A6300705AAA /* HighlightViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D965BA31FA79A6300705AAA /* HighlightViewController.swift */; };
6D965BA81FA7B6DD00705AAA /* PlayerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D965BA71FA7B6DD00705AAA /* PlayerViewController.swift */; };
6DEE676E2115989400FF2EB4 /* ListTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DEE676D2115989400FF2EB4 /* ListTableViewController.swift */; };
6FDABBFFC80A25A8DEEFE3E3 /* Pods_Demo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DB13C5E8ADDE29D425A8FEC /* Pods_Demo.framework */; };
/* End PBXBuildFile section */

Expand All @@ -33,6 +34,7 @@
6D965BA11FA7974100705AAA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
6D965BA31FA79A6300705AAA /* HighlightViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HighlightViewController.swift; sourceTree = "<group>"; };
6D965BA71FA7B6DD00705AAA /* PlayerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerViewController.swift; sourceTree = "<group>"; };
6DEE676D2115989400FF2EB4 /* ListTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListTableViewController.swift; sourceTree = "<group>"; };
8DB13C5E8ADDE29D425A8FEC /* Pods_Demo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Demo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
A67AFDB912F87244FCB0C745 /* Pods-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Demo.release.xcconfig"; path = "Pods/Target Support Files/Pods-Demo/Pods-Demo.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -88,6 +90,7 @@
6D37D92B1FA8852C00B33EA0 /* GroupViewController.swift */,
6D0C91581FA8BF350073554B /* ArticleViewController.swift */,
6D1F24511FACC2E6002E41FF /* CardContentViewController.swift */,
6DEE676D2115989400FF2EB4 /* ListTableViewController.swift */,
);
path = ViewControllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -249,6 +252,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6DEE676E2115989400FF2EB4 /* ListTableViewController.swift in Sources */,
6D37D92C1FA8852C00B33EA0 /* GroupViewController.swift in Sources */,
6D436D571F9BD34600B05FA6 /* AppDelegate.swift in Sources */,
6D0C91591FA8BF350073554B /* ArticleViewController.swift in Sources */,
Expand Down Expand Up @@ -398,7 +402,7 @@
INFOPLIST_FILE = Demo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = paolocuscela.Cards;
PRODUCT_BUNDLE_IDENTIFIER = it.paolocuscela.Cards;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.0;
Expand All @@ -417,7 +421,7 @@
INFOPLIST_FILE = Demo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = paolocuscela.Cards;
PRODUCT_BUNDLE_IDENTIFIER = it.paolocuscela.Cards;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.0;
Expand Down
8 changes: 8 additions & 0 deletions Demo/Demo.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Loading

0 comments on commit f3befb4

Please sign in to comment.