Skip to content

Instantly share code, notes, and snippets.

@fumiyasac
Last active December 11, 2017 11:46
Show Gist options
  • Save fumiyasac/d874ee60e7fb1c7d91cf90fe92c2f95f to your computer and use it in GitHub Desktop.
Save fumiyasac/d874ee60e7fb1c7d91cf90fe92c2f95f to your computer and use it in GitHub Desktop.
Swift4にも対応したUI系のライブラリを活用して面白い動きを入れてみたサンプル実装まとめ ref: https://qiita.com/fumiyasac@github/items/48617348ef7f18494d38
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = navigationController as? ScrollingNavigationController {
navigationController.followScrollView(articleTableView, delay: 44.0)
//MEMO: ScrollingNavigationControllerDelegateを利用する際に必要な宣言
//navigationController.scrollingNavbarDelegate = self
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let navigationController = self.navigationController as? ScrollingNavigationController {
navigationController.stopFollowingScrollView()
}
}
import UIKit
import FSPagerView
class CoverFlowSliderViewController: UIViewController {
//カバーフロー形式のギャラリーを表示するためのFSPagerView
@IBOutlet weak fileprivate var coverFlowSliderView: FSPagerView! {
didSet {
self.coverFlowSliderView.register(FSPagerViewCell.self, forCellWithReuseIdentifier: "cell")
}
}
fileprivate var coverflowContents: [Coverflow] = [] {
didSet {
self.coverFlowSliderView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
setupCoverFlowSliderView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK: - Private Functions
private func setupCoverFlowSliderView() {
//UICollectionViewとほとんど同じ感じで設定ができる
coverFlowSliderView.delegate = self
coverFlowSliderView.dataSource = self
coverFlowSliderView.isInfinite = true
coverFlowSliderView.itemSize = CGSize(width: 180, height: 120)
coverFlowSliderView.interitemSpacing = 16
coverFlowSliderView.transformer = FSPagerViewTransformer(type: .coverFlow)
coverflowContents = Coverflow.getSampleData()
}
}
//MARK: - FSPagerViewDataSource, FSPagerViewDelegate
extension CoverFlowSliderViewController: FSPagerViewDataSource, FSPagerViewDelegate {
func numberOfItems(in pagerView: FSPagerView) -> Int {
return coverflowContents.count
}
func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
let coverflow = coverflowContents[index]
cell.contentView.layer.shadowOpacity = 0.4
cell.contentView.layer.opacity = 0.86
cell.imageView?.image = coverflow.thumbnail
cell.imageView?.contentMode = .scaleAspectFill
cell.imageView?.clipsToBounds = true
return cell
}
func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
pagerView.deselectItem(at: index, animated: true)
pagerView.scrollToItem(at: index, animated: true)
}
}
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'MixedUILibrariesSample' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for MixedUILibrariesSample
# UILaibrary
pod 'FlexibleSteppedProgressBar'
pod 'VegaScrollFlowLayout'
pod 'FSPagerView'
pod 'ParallaxHeader', '~> 2.0.0'
pod 'AMScrollingNavbar'
pod 'Cosmos', '~> 12.0'
pod 'ActiveLabel', :git => '[email protected]:optonaut/ActiveLabel.swift.git', :branch => 'master'
pod 'FontAwesome.swift', :git => 'https://github.com/thii/FontAwesome.swift', :branch => 'swift-4.0'
end
func setCell(_ article: Article) {
let hashtagString = article.hashTags.joined(separator: " ")
//ハッシュタグの行の高さを調節する
let hashtagParagraphStyle = NSMutableParagraphStyle.init()
hashtagParagraphStyle.minimumLineHeight = 20
let hashtagAttributedText = NSMutableAttributedString.init(string: hashtagString)
hashtagAttributedText.addAttribute(NSAttributedStringKey.paragraphStyle, value: hashtagParagraphStyle, range: NSMakeRange(0, hashtagAttributedText.length))
//リンク表示にするラベルの種類を設定する
articleHashtagLabel.enabledTypes = [.hashtag]
//ハッシュタグを表示する
articleHashtagLabel.attributedText = hashtagAttributedText
//それぞれのハッシュタグをクリックした時に発火するアクションを設定する
articleHashtagLabel.handleHashtagTap { hashtag in
print("押されたハッシュタグ:\(hashtag)")
}
}
import UIKit
import VegaScrollFlowLayout
class ListViewController: UIViewController {
//UI部品の配置
@IBOutlet weak var listCollectionView: UICollectionView!
fileprivate var listContents: [List] = [] {
didSet {
self.listCollectionView.reloadData()
}
}
//CollectionView表示の隙間やサイズに関する設定
private let itemHeight: CGFloat = 180
private let lineSpacing: CGFloat = 15
private let spaceInset: CGFloat = 15
private let topInset: CGFloat = 10
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBar()
setupListCollectionViewCell()
setupListCollectionViewLayout()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK: - Private Functiom
private func setupNavigationBar() {
removeBackButtonText()
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationItem.title = "クリスマスに食べたいディナー"
}
private func setupListCollectionViewCell() {
listCollectionView.delegate = self
listCollectionView.dataSource = self
listCollectionView.registerCustomCell(ListCollectionViewCell.self)
//リスト用のUICollectionViewの下部をセルの高さ分マージンを開ける
listCollectionView.contentInset.bottom = itemHeight
listContents = List.getSampleData()
}
private func setupListCollectionViewLayout() {
//表示するセルのサイズや隙間に関する値の設定をする
guard let layout = listCollectionView.collectionViewLayout as? VegaScrollFlowLayout else { return }
layout.minimumLineSpacing = lineSpacing
layout.sectionInset = UIEdgeInsets(top: topInset, left: 0, bottom: 0, right: 0)
let itemWidth = UIScreen.main.bounds.width - 2 * spaceInset
layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
listCollectionView.collectionViewLayout.invalidateLayout()
}
}
//MARK: - UICollectionViewDataSource, UICollectionViewDelegate
extension ListViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCustomCell(with: ListCollectionViewCell.self, indexPath: indexPath)
let list = listContents[indexPath.row]
cell.setCell(list)
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return listContents.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "ToArticleViewController", sender: self)
}
}
import UIKit
import FlexibleSteppedProgressBar
class MainViewController: UIViewController {
//ステップインジケータ表示用部品のOutlet
@IBOutlet weak var stepIndicator: FlexibleSteppedProgressBar!
・・・(省略)・・・
//ContainerViewにEmbedしたUIPageViewControllerのインスタンスを保持する
fileprivate var pageViewController: UIPageViewController?
//ページングして表示させるViewControllerを保持する配列
fileprivate var tutorialControllerLists = [TutorialBaseViewController]()
//チュートリアル画面に表示する要素
private let tutorialContents: [Tutorial] = Tutorial.getSampleData()
override func viewDidLoad() {
super.viewDidLoad()
・・・(省略)・・・
setupStepIndicator()
・・・(省略)・・・
}
・・・(省略)・・・
//ステップインジケータ表示の初期表示に関するセッティングをするメソッド
private func setupStepIndicator() {
stepIndicator.delegate = self
//ステップインジケータの表示数を設定する
stepIndicator.numberOfPoints = 3
//ステップインジケータの線幅を設定する
stepIndicator.lineHeight = 4
//ステップインジケータの配色及び外枠を設定する
stepIndicator.selectedOuterCircleLineWidth = 4.0
stepIndicator.selectedOuterCircleStrokeColor = UIColor.orange
stepIndicator.currentSelectedCenterColor = UIColor.white
//ステップインジケータのアニメーション秒数を設定する
stepIndicator.stepAnimationDuration = 0.26
//ステップインジケータの現在位置を設定する
stepIndicator.currentIndex = 0
}
・・・(省略)・・・
}
・・・(省略)・・・
// MARK - FlexibleSteppedProgressBarDelegate
extension MainViewController: FlexibleSteppedProgressBarDelegate {
//ステップインジケータを選択した際に実行されるメソッド
func progressBar(_ progressBar: FlexibleSteppedProgressBar,
didSelectItemAtIndex index: Int) {
stepIndicator.currentIndex = index
}
//ステップインジケータを選択の選択可否を設定するメソッド
func progressBar(_ progressBar: FlexibleSteppedProgressBar,
canSelectItemAtIndex index: Int) -> Bool {
return false
}
//ステップインジケータに表示される文言を表示するメソッド
func progressBar(_ progressBar: FlexibleSteppedProgressBar,
textAtIndex index: Int, position: FlexibleSteppedProgressBarTextLocation) -> String {
//特に必要ない場合にはカラッポの文字列にする
return ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment