Skip to content

Commit

Permalink
Merge pull request #25 from aryaxt/RemoveForceUnwrapping
Browse files Browse the repository at this point in the history
get rid of force unwrapping code
  • Loading branch information
aryaxt committed Jun 8, 2016
2 parents 870ab59 + edbcbc9 commit 2ce4d5b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ScrollPager.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = 'ScrollPager'
s.version = '0.6'
s.version = '0.7'
s.summary = 'A fully featured scroll pager similar to the one in flipboard, fully configurable through storyboard'
s.homepage = 'https://github.com/aryaxt/ScrollPager'
s.license = {
:type => 'MIT',
:file => 'License.txt'
}
s.author = {'Aryan Ghassemi' => 'https://github.com/aryaxt/ScrollPager'}
s.source = {:git => 'https://github.com/aryaxt/ScrollPager.git', :tag => '0.6'}
s.source = {:git => 'https://github.com/aryaxt/ScrollPager.git', :tag => '0.7'}
s.platform = :ios, '8.0'
s.source_files = 'ScrollPager/Source/*.{swift}'
s.framework = 'Foundation', 'UIKit'
Expand Down
40 changes: 21 additions & 19 deletions ScrollPager/Source/ScrollPager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ import UIKit
}

private func addViews(segmentViews: [UIView]) {
for view in scrollView!.subviews {
guard let scrollView = scrollView else { fatalError("trying to add views but the scrollView is nil") }

for view in scrollView.subviews {
view.removeFromSuperview()
}

for i in 0..<segmentViews.count {
let view = segmentViews[i]
scrollView!.addSubview(view)
scrollView.addSubview(view)
views.append(view)
}
}
Expand Down Expand Up @@ -196,30 +198,30 @@ import UIKit

UIView.animateWithDuration(animated ? NSTimeInterval(animationDuration) : 0.0, delay: 0.0, options: .CurveEaseOut, animations: { [weak self] in

let width = self!.frame.size.width / CGFloat(self!.buttons.count)
let button = self!.buttons[index]
guard let strongSelf = self else { return }
let width = strongSelf.frame.size.width / CGFloat(strongSelf.buttons.count)
let button = strongSelf.buttons[index]

self?.redrawButtons()
strongSelf.redrawButtons()

if self!.indicatorSizeMatchesTitle {
let string: NSString? = button.titleLabel?.text as NSString?
let size = string?.sizeWithAttributes([NSFontAttributeName: button.titleLabel!.font])
let x = width * CGFloat(index) + ((width - size!.width) / CGFloat(2))
self!.indicatorView.frame = CGRectMake(x, self!.frame.size.height - self!.indicatorHeight, size!.width, self!.indicatorHeight)
if strongSelf.indicatorSizeMatchesTitle {
guard let string = button.titleLabel?.text else { fatalError("missing title on button, title is required for width calculation") }
guard let font = button.titleLabel?.font else { fatalError("missing dont on button, title is required for width calculation") }
let size = string.sizeWithAttributes([NSFontAttributeName: font])
let x = width * CGFloat(index) + ((width - size.width) / CGFloat(2))
strongSelf.indicatorView.frame = CGRectMake(x, strongSelf.frame.size.height - strongSelf.indicatorHeight, size.width, strongSelf.indicatorHeight)
}
else {
self!.indicatorView.frame = CGRectMake(width * CGFloat(index), self!.frame.size.height - self!.indicatorHeight, button.frame.size.width, self!.indicatorHeight)
strongSelf.indicatorView.frame = CGRectMake(width * CGFloat(index), strongSelf.frame.size.height - strongSelf.indicatorHeight, button.frame.size.width, strongSelf.indicatorHeight)
}

if self!.scrollView != nil && moveScrollView {
self!.scrollView?.contentOffset = CGPointMake(CGFloat(index) * self!.scrollView!.frame.size.width, 0)
if let scrollView = strongSelf.scrollView where moveScrollView {
scrollView.contentOffset = CGPointMake(CGFloat(index) * scrollView.frame.size.width, 0)
}

}, completion: { [weak self] finished in
// Storyboard crashes on here for some odd reasons, do a nil check
if self != nil {
self!.animationInProgress = false
}
self?.animationInProgress = false
})
}

Expand All @@ -230,11 +232,11 @@ import UIKit
moveToIndex(selectedIndex, animated: false, moveScrollView: false)
}

if scrollView != nil {
scrollView!.contentSize = CGSizeMake(scrollView!.frame.size.width * CGFloat(buttons.count), scrollView!.frame.size.height)
if let scrollView = scrollView {
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(buttons.count), scrollView.frame.size.height)

for i in 0..<views.count {
views[i].frame = CGRectMake(scrollView!.frame.size.width * CGFloat(i), 0, scrollView!.frame.size.width, scrollView!.frame.size.height)
views[i].frame = CGRectMake(scrollView.frame.size.width * CGFloat(i), 0, scrollView.frame.size.width, scrollView.frame.size.height)
}
}
}
Expand Down

0 comments on commit 2ce4d5b

Please sign in to comment.