Skip to content
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
4 changes: 4 additions & 0 deletions ScrollTabPageViewController.xcodeproj/project.pbxproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -271,21 +271,25 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = ScrollTabPageViewController/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
4C8C50DD1C11B617008A69C9 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = ScrollTabPageViewController/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = jp.vasily.ScrollTabPageViewController;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Empty file.
3 changes: 1 addition & 2 deletions ScrollTabPageViewController/AppDelegate.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
}

Empty file.
93 changes: 56 additions & 37 deletions ScrollTabPageViewController/Classes/ContentsView.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,36 @@ import UIKit

class ContentsView: UIView {

// 選択されているtabボタンのindex
var currentIndex: Int = 0
var tabButtonPressedBlock: ((index: Int) -> Void)?
var scrollDidChangedBlock: ((scroll: CGFloat, shouldScroll: Bool) -> Void)?

private var scrollStart: CGFloat = 0.0

@IBOutlet private weak var contentView: UIView!
@IBOutlet private weak var containerView: UIView!
@IBOutlet private weak var scrollView: UIScrollView!
@IBOutlet var tabButtons: [UIButton]!


var tabButtonPressedBlock: ((_ index: Int) -> Void)?
var scrollDidChangedBlock: ((_ scroll: CGFloat, _ shouldScroll: Bool) -> Void)?

// スクロール開始時点の初期値
var scrollStart: CGFloat = 0.0

@IBOutlet weak var contentView: UIView!
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var segmentedControlHeight: NSLayoutConstraint!
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!

sharedInit()
}

override init(frame: CGRect) {
super.init(frame: frame)

sharedInit()
}

private func sharedInit() {
NSBundle.mainBundle().loadNibNamed("ContentsView", owner: self, options: nil)
Bundle.main.loadNibNamed("ContentsView", owner: self, options: nil)
addSubview(contentView)

setupConstraints()
self.setupConstraints()

scrollView.delegate = self
scrollView.scrollsToTop = false
Expand All @@ -49,31 +51,40 @@ class ContentsView: UIView {

extension ContentsView {

private func setupConstraints() {
let topConstraint = NSLayoutConstraint(item: contentView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0.0)
// 制約を更新
func setupConstraints() {
let topConstraint = NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 20.0)

let bottomConstraint = NSLayoutConstraint(item: contentView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
let bottomConstraint = NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0)

let leftConstraint = NSLayoutConstraint(item: contentView, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0.0)
let leftConstraint = NSLayoutConstraint(item: contentView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0)

let rightConstraint = NSLayoutConstraint(item: contentView, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1.0, constant: 0.0)
let rightConstraint = NSLayoutConstraint(item: contentView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0)

let constraints = [topConstraint, bottomConstraint, leftConstraint, rightConstraint]

contentView.translatesAutoresizingMaskIntoConstraints = false
addConstraints(constraints)
}

private func randomColor() -> UIColor {
let red = Float(arc4random_uniform(255)) / 255.0
let green = Float(arc4random_uniform(255)) / 255.0
let blue = Float(arc4random_uniform(255)) / 255.0
return UIColor(colorLiteralRed: red, green: green, blue: blue, alpha: 1.0)
/**
ランダムな色を取得
- returns: ランダムな色
*/
func randomColor() -> UIColor {
let red = CGFloat(arc4random_uniform(255)) / 255.0
let green = CGFloat(arc4random_uniform(255)) / 255.0
let blue = CGFloat(arc4random_uniform(255)) / 255.0
return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}


/**
tabボタンのindex番号を更新
- parameter index: 更新しようとしているindex番号
- parameter animated: アニメーションするかのBOOL
*/
func updateCurrentIndex(index: Int, animated: Bool) {
tabButtons[currentIndex].backgroundColor = UIColor.whiteColor()
tabButtons[index].backgroundColor = UIColor(red: 0.88, green: 1.0, blue: 0.87, alpha: 1.0)
segmentedControl.selectedSegmentIndex = index
currentIndex = index
}
}
Expand All @@ -83,13 +94,23 @@ extension ContentsView {

extension ContentsView: UIScrollViewDelegate {

func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y > 0.0 || frame.minY < 0.0 {
scrollDidChangedBlock?(scroll: scrollView.contentOffset.y, shouldScroll: true)
/**
contentsViewへのスクロールを検知
- parameter scrollView: scrollView
*/
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y > statusBarHeight {
scrollDidChangedBlock?(scrollView.contentOffset.y, true)
scrollView.contentOffset.y = statusBarHeight
} else if scrollView.contentOffset.y > 0.0 {
scrollDidChangedBlock?(scrollView.contentOffset.y, true)
scrollView.contentOffset.y = 0.0
} else if frame.minY < 0.0 {
scrollDidChangedBlock?(scrollView.contentOffset.y, true)
scrollView.contentOffset.y = 0.0
} else {
let scroll = scrollView.contentOffset.y - scrollStart
scrollDidChangedBlock?(scroll: scroll, shouldScroll: false)
scrollDidChangedBlock?(scroll, false)
scrollStart = scrollView.contentOffset.y
}
}
Expand All @@ -99,13 +120,11 @@ extension ContentsView: UIScrollViewDelegate {
// MARK: - IBAction

extension ContentsView {

@IBAction private func touchButtonTouchUpInside(button: UIButton) {
@IBAction private func touchButtonTouchUpInside(_ sender: UIButton) {
containerView.backgroundColor = randomColor()
}

@IBAction private func tabButtonTouchUpInside(button: UIButton) {
tabButtonPressedBlock?(index: button.tag)
updateCurrentIndex(button.tag, animated: true)
@IBAction func segmentValueChanged(_ sender: UISegmentedControl) {
tabButtonPressedBlock?(sender.selectedSegmentIndex)
updateCurrentIndex(index: sender.selectedSegmentIndex, animated: true)
}
}
Loading