Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mindz-eye committed Dec 2, 2017
2 parents 3be3761 + f32ef93 commit 43670e9
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 27 deletions.
15 changes: 14 additions & 1 deletion Example/MYTableViewIndex.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,18 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 0910;
ORGANIZATIONNAME = "Makarov Yury";
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = WDCQ6B387N;
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = WDCQ6B387N;
LastSwiftMigration = 0800;
TestTargetID = 607FACCF1AFB9204008FA782;
};
Expand Down Expand Up @@ -525,12 +528,16 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = WDCQ6B387N;
INFOPLIST_FILE = MYTableViewIndex/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "my.MYTableViewIndex-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.0;
};
name = Debug;
Expand All @@ -541,12 +548,16 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = WDCQ6B387N;
INFOPLIST_FILE = MYTableViewIndex/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "my.MYTableViewIndex-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_VERSION = 4.0;
};
name = Release;
Expand All @@ -555,6 +566,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 5EEB8E8AC054B8E0F15CA367 /* Pods-MYTableViewIndex_Tests.debug.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = WDCQ6B387N;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -575,6 +587,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 5947C14CE9C89E07B48DE0C1 /* Pods-MYTableViewIndex_Tests.release.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = WDCQ6B387N;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0910"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
15 changes: 12 additions & 3 deletions Example/MYTableViewIndex/CollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,21 @@ class CollectionViewController : UICollectionViewController, UICollectionViewDel
if sectionIndex == NSNotFound {
return false
}
guard let collectionView = collectionView else {
return false;
}
let indexPath = IndexPath(row: 0, section: sectionIndex)
guard let attrs = collectionView!.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath) else {
guard let attrs = collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath) else {
return false
}
let yOffset = min(attrs.frame.origin.y, collectionView!.contentSize.height - collectionView!.frame.height + collectionView!.contentInset.top)
collectionView!.contentOffset = CGPoint(x: 0, y: yOffset - collectionView!.contentInset.top)
var contentInset: UIEdgeInsets
if #available(iOS 11.0, *) {
contentInset = collectionView.adjustedContentInset
} else {
contentInset = collectionView.contentInset
}
let yOffset = min(attrs.frame.origin.y, collectionView.contentSize.height - collectionView.frame.height + contentInset.top)
collectionView.contentOffset = CGPoint(x: 0, y: yOffset - contentInset.top)

return true
}
Expand Down
2 changes: 1 addition & 1 deletion Example/MYTableViewIndex/Examples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class LargeFontExample : SearchExample {
override func setupTableIndexController(_ tableIndexController: TableViewIndexController) {
super.setupTableIndexController(tableIndexController)

tableIndexController.tableViewIndex.font = UIFont.boldSystemFont(ofSize: 20.0)
tableIndexController.tableViewIndex.font = UIFont.boldSystemFont(ofSize: 15.0)
}
}

Expand Down
6 changes: 5 additions & 1 deletion Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion MYTableViewIndex.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'MYTableViewIndex'
s.version = '0.5.0'
s.version = '0.6.0'
s.summary = 'A pixel perfect replacement for UITableView section index, written in Swift.'

s.description = <<-DESC
Expand Down
4 changes: 3 additions & 1 deletion MYTableViewIndex/Private/BackgroundView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class BackgroundView : UIView {
}

private func commonInit() {
backgroundColor = UIColor.white.withAlphaComponent(0.9)
if #available(iOS 11.0, *) {} else {
backgroundColor = UIColor.white.withAlphaComponent(0.9)
}
isUserInteractionEnabled = false
}
}
22 changes: 22 additions & 0 deletions MYTableViewIndex/Private/Utils/UIScrollView+Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// UIScrollView+Helpers.swift
// MYTableViewIndex
//
// Created by Makarov Yury on 02/12/2017.
// Copyright © 2017 Makarov Yury. All rights reserved.
//

import UIKit

extension UIScrollView {

var my_effectiveContentInset: UIEdgeInsets {
get {
if #available(iOS 11.0, *) {
return self.adjustedContentInset
} else {
return self.contentInset
}
}
}
}
1 change: 1 addition & 0 deletions MYTableViewIndex/Public/Items/SearchItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ open class SearchItem : UIView {
override public init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.clear
accessibilityLabel = NSLocalizedString("Search", comment: "Accessibility title for search icon")
}

required public init?(coder aDecoder: NSCoder) {
Expand Down
73 changes: 60 additions & 13 deletions MYTableViewIndex/Public/TableViewIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class TableViewIndex : UIControl {
/// Delegate for the table index object. See TableViewIndexDelegate protocol for details.
@IBOutlet public weak var delegate: TableViewIndexDelegate?

/// Background view is displayed below the index items and can customized with any UIView.
/// Background view is displayed below the index items and can be customized with any UIView.
/// If not set or set to nil, creates a default view which mimics the system index appearance.
public var backgroundView: UIView! {
didSet {
Expand Down Expand Up @@ -117,6 +117,11 @@ open class TableViewIndex : UIControl {

isExclusiveTouch = true
isMultipleTouchEnabled = false
isAccessibilityElement = true
accessibilityTraits = UIAccessibilityTraitAdjustable
accessibilityLabel = NSLocalizedString("Table index", comment: "Accessibility title for the section index control")

updateAccessibilityValue()
}

// MARK: - Updates
Expand Down Expand Up @@ -155,6 +160,41 @@ open class TableViewIndex : UIControl {
item.applyAttributes(style)
}
}

private func selectIndex(_ index: Int) {
guard index != currentIndex else { return }

currentIndex = index

updateAccessibilityValue()

if let delegate = self.delegate
, delegate.responds(to: #selector(TableViewIndexDelegate.tableViewIndex(_:didSelect:at:))) {

let shouldProduceFeedback = delegate.tableViewIndex!(self, didSelect: items[index], at: index)
if shouldProduceFeedback {
notifyFeedbackGenerator()
}
}
}

private func updateAccessibilityValue() {
guard currentIndex >= 0 && currentIndex < items.count else { return }

let currentItem = items[currentIndex]

let titleText: String
if let labelText = currentItem.accessibilityLabel {
titleText = labelText
} else if let labelText = (currentItem as? UILabel)?.text {
titleText = labelText
} else {
titleText = String.localizedStringWithFormat(NSLocalizedString("Section %d", comment: "Accessibility title for a numbered section"), currentIndex + 1)
}

let selectedText = NSLocalizedString("Selected", comment: "Accessibility title for the selected state")
accessibilityValue = "\(titleText), \(selectedText)"
}

// MARK: - Layout

Expand Down Expand Up @@ -236,7 +276,7 @@ open class TableViewIndex : UIControl {
// MARK: - Touches

private var currentTouch: UITouch?
private var currentIndex: Int?
private var currentIndex: Int = 0

override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first , bounds.contains(touch.location(in: self)) {
Expand Down Expand Up @@ -284,21 +324,12 @@ open class TableViewIndex : UIControl {
if idx == currentIndex {
return
}
currentIndex = idx

if let delegate = self.delegate
, delegate.responds(to: #selector(TableViewIndexDelegate.tableViewIndex(_:didSelect:at:))) {

let shouldProduceFeedback = delegate.tableViewIndex!(self, didSelect: items[idx], at: idx)
if shouldProduceFeedback {
notifyFeedbackGenerator()
}
}

selectIndex(idx)
}

private func finalizeTouch() {
currentTouch = nil
currentIndex = nil
isHighlighted = false
cleanupFeedbackGenerator()
}
Expand Down Expand Up @@ -332,6 +363,22 @@ open class TableViewIndex : UIControl {
feedbackGeneratorInstance = nil
}
}

// MARK: - Accessibility support

open override func accessibilityIncrement() {
let newIndex = currentIndex - 1
if newIndex >= 0 {
selectIndex(newIndex)
}
}

open override func accessibilityDecrement() {
let newIndex = currentIndex + 1
if newIndex < items.count {
selectIndex(newIndex)
}
}
}

// MARK: - Protocols
Expand Down
21 changes: 17 additions & 4 deletions MYTableViewIndex/Public/TableViewIndexController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class TableViewIndexController : NSObject {

private enum ObservedKeyPaths: String {
case contentInset
case adjustedContentInset
case bounds
case center
}
Expand All @@ -48,9 +49,16 @@ public class TableViewIndexController : NSObject {
guard let scrollView = scrollView else {
return
}
var insetKeypath: String
if #available(iOS 11.0, *) {
insetKeypath = ObservedKeyPaths.adjustedContentInset.rawValue
} else {
insetKeypath = ObservedKeyPaths.contentInset.rawValue
}

let keyPaths = [ObservedKeyPaths.bounds.rawValue,
ObservedKeyPaths.center.rawValue,
ObservedKeyPaths.contentInset.rawValue]
insetKeypath]

observer = KeyValueObserver(object: scrollView, keyPaths: keyPaths, handler: {[weak self] keyPath in
self?.layout()
Expand Down Expand Up @@ -85,8 +93,13 @@ public class TableViewIndexController : NSObject {

let convertedFrame = parentView.convert(frame, from: nil)

var inset = scrollView.contentInset
inset.bottom = (scrollView.frame.maxY - convertedFrame.minY)
var safeAreaInsets = UIEdgeInsets()
if #available(iOS 11.0, *) {
safeAreaInsets = parentView.safeAreaInsets
}

var inset = scrollView.my_effectiveContentInset
inset.bottom = max(scrollView.frame.maxY - convertedFrame.minY, safeAreaInsets.bottom)

UIView.animate(withDuration: duration, animations: {
UIView.setAnimationCurve(UIViewAnimationCurve(rawValue: curve)!)
Expand All @@ -106,7 +119,7 @@ public class TableViewIndexController : NSObject {
guard let scrollView = scrollView else {
return
}
layout(with: scrollView.contentInset)
layout(with: scrollView.my_effectiveContentInset)
}

private func layout(with inset: UIEdgeInsets) {
Expand Down

0 comments on commit 43670e9

Please sign in to comment.