Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/leak #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
129 changes: 59 additions & 70 deletions ISScrollViewPage/ISScrollViewPage/ISScrollViewPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,27 @@ import UIKit
}

@objc public protocol ISScrollViewPageDelegate {
func scrollViewPageDidChanged(_ scrollViewPage:ISScrollViewPage,index:Int);
@objc optional func scrollViewPageDidScroll(_ scrollView:UIScrollView);
func scrollViewPageDidChanged(_ scrollViewPage:ISScrollViewPage, index: Int);
@objc optional func scrollViewPageDidScroll(_ scrollView: UIScrollView);
}

open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {

open var viewControllers:[UIViewController]?
open var viewControllers: [UIViewController]?
open var views = [UIView]()
open var scrollViewPageDelegate:ISScrollViewPageDelegate?
open weak var scrollViewPageDelegate: ISScrollViewPageDelegate?
open var count: Int {
get {
let viewsCount = views.count
let controllersCount = viewControllers != nil ? viewControllers!.count : 0
return viewsCount + controllersCount
}
return views.count + (viewControllers?.count ?? 0)
}
var lastIndex = 0
var enableBouces:Bool?
var enablePaging:Bool?
var fillContent:Bool?
open var scrollViewPageType:ISScrollViewPageType!
var isLoaded:Bool!
var sizeOfViews:CGFloat = 0
var enableBouces: Bool?
var enablePaging: Bool?
var fillContent: Bool?
open var scrollViewPageType: ISScrollViewPageType!
var isLoaded: Bool!
var sizeOfViews: CGFloat = 0

open func getScrollViewPageTypeFromInt(_ value:Int) -> ISScrollViewPageType {
open func getScrollViewPageTypeFromInt(_ value: Int) -> ISScrollViewPageType {
switch(value) {
case 1:
return .horizontally
Expand Down Expand Up @@ -74,24 +70,22 @@ open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {
//MARK: UIScrollViewDelegate

open func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let scrollViewPageDelegate = self.scrollViewPageDelegate {
scrollViewPageDelegate.scrollViewPageDidScroll?(scrollView)
}
scrollViewPageDelegate?.scrollViewPageDidScroll?(scrollView)
}

open func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

var index:Int

switch (self.scrollViewPageType!) {
switch (scrollViewPageType!) {

case .horizontally:

let frame = self.frame.width

if contentOffset.x != 0 {
index = Int(self.contentOffset.x / frame)
}else{
index = Int(contentOffset.x / frame)
} else{
index = 0;
}

Expand All @@ -100,8 +94,8 @@ open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {
let frame = self.frame.height

if contentOffset.y != 0 {
index = Int(self.contentOffset.y / frame)
}else{
index = Int(contentOffset.y / frame)
} else {
index = 0;
}

Expand All @@ -118,9 +112,9 @@ open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {

//MARK: Public Functions

open func goToIndex(_ index:Int, animated:Bool){
open func goToIndex(_ index: Int, animated: Bool){

let countList = (self.viewControllers?.count)! > 0 ? self.viewControllers?.count : self.views.count
let countList = (viewControllers?.count)! > 0 ? viewControllers?.count : views.count

if index >= countList! {

Expand All @@ -133,7 +127,7 @@ open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {

case .horizontally:

let frameWidth = Int(self.frame.width)
let frameWidth = Int(frame.width)
let widthOfContentOffset = index * frameWidth

self.setContentOffset(CGPoint(x: CGFloat(widthOfContentOffset), y: 0), animated: animated)
Expand All @@ -155,87 +149,87 @@ open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {

}

open func setFillContent(_ fillContent:Bool) {
open func setFillContent(_ fillContent: Bool) {
self.fillContent = fillContent
setupLayout(self.scrollViewPageType)
setupLayout(scrollViewPageType)
}

open func setControllers(_ viewControllers:[UIViewController]){
open func setControllers(_ viewControllers: [UIViewController]) {
self.viewControllers! = viewControllers;
setupLayout(self.scrollViewPageType)
setupLayout(scrollViewPageType)
}

open func addCustomView(_ view:UIView) {
self.views.append(view)
setupLayout(self.scrollViewPageType)
open func addCustomView(_ view: UIView) {
views.append(view)
setupLayout(scrollViewPageType)
}

open func setCustomViews(_ views:[UIView]){
open func setCustomViews(_ views: [UIView]) {
self.views = views
setupLayout(self.scrollViewPageType)
setupLayout(scrollViewPageType)
}

open func removeCustomViewAtIndex(_ index:Int) {
if !views.isEmpty{
self.views.remove(at: index)
open func removeCustomViewAtIndex(_ index: Int) {
if !views.isEmpty {
views.remove(at: index)
}
setupLayout(self.scrollViewPageType)
setupLayout(scrollViewPageType)
}

open func removeCustomView(_ mediaView:UIView) {
open func removeCustomView(_ mediaView: UIView) {
if !views.isEmpty{
if let index = (views).firstIndex(of: mediaView) {
self.views.remove(at: index)
views.remove(at: index)
}
}
setupLayout(self.scrollViewPageType)
setupLayout(scrollViewPageType)
}

open func removeAllViews() {
views.removeAll()
viewControllers?.removeAll()
setupLayout(self.scrollViewPageType)
setupLayout(scrollViewPageType)
}

open func setEnableBounces(_ enableBounces:Bool){
self.bounces = enableBounces
open func setEnableBounces(_ enableBounces: Bool) {
bounces = enableBounces
}

open func setPaging(_ pagingEnabled:Bool){
self.enablePaging = pagingEnabled
open func setPaging(_ pagingEnabled: Bool) {
enablePaging = pagingEnabled
}

open func setScrollViewPageType(_ value:Int) {
self.scrollViewPageType = getScrollViewPageTypeFromInt(value);
open func setScrollViewPageType(_ value: Int) {
scrollViewPageType = getScrollViewPageTypeFromInt(value);
}

//MARK: Private Functions

fileprivate func initScrollView() {
super.delegate = self
self.viewControllers = []
self.views = []
self.scrollViewPageType = ISScrollViewPageType.horizontally
viewControllers = []
views = []
scrollViewPageType = ISScrollViewPageType.horizontally
}

fileprivate func removeSubviews() {
for view in self.subviews {
for view in subviews {
view.removeFromSuperview()
}
}

fileprivate func setupLayout (_ scrollViewPageType:ISScrollViewPageType){
fileprivate func setupLayout (_ scrollViewPageType: ISScrollViewPageType) {

sizeOfViews = 0
removeSubviews()

var list:[AnyObject] = []
var list: [AnyObject] = []

if !viewControllers!.isEmpty {
list = viewControllers!
}else if !views.isEmpty{
} else if !views.isEmpty {
list = views
}else {
} else {
return
}

Expand All @@ -245,19 +239,18 @@ open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {

if let objectView = object as? UIView {
build(list.count,index:i,objectView:objectView,scrollViewPageType: scrollViewPageType)
}else {
} else {
let objectView = list[i] as! UIViewController
build(list.count,index:i,objectView:objectView.view,scrollViewPageType: scrollViewPageType)
build(list.count, index: i, objectView:objectView.view, scrollViewPageType: scrollViewPageType)
}

}

self.isPagingEnabled = enablePaging!

}

fileprivate func build(_ numberOfViews:Int,index:Int,objectView:UIView,scrollViewPageType:ISScrollViewPageType) {
var frame = self.frame
fileprivate func build(_ numberOfViews: Int, index: Int, objectView: UIView, scrollViewPageType: ISScrollViewPageType) {
var frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.width, height: self.frame.height)
let view:UIView = UIView()
view.isUserInteractionEnabled = true
view.clipsToBounds = true
Expand All @@ -266,7 +259,7 @@ open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {
case .horizontally:
sizeOfViews = sizeOfViews + objectView.frame.size.width

frame.origin.x = CGFloat(fillContent == true ? self.frame.size.width * CGFloat(index) : index == 0 ? 0 : sizeOfViews - objectView.frame.size.width);
frame.origin.x = CGFloat(fillContent == true ? self.frame.size.width * CGFloat(index) : index == 0 ? 0 : sizeOfViews - objectView.frame.size.width)
frame.size = fillContent == true ? self.frame.size : objectView.frame.size
frame.origin.y = 0

Expand All @@ -275,19 +268,15 @@ open class ISScrollViewPage: UIScrollView, UIScrollViewDelegate {
case .vertically:
sizeOfViews = sizeOfViews + objectView.frame.size.height

frame.origin.y = CGFloat(fillContent == true ? self.frame.size.height * CGFloat(index) : index == 0 ? 0 : sizeOfViews - objectView.frame.size.height);
frame.origin.y = CGFloat(fillContent == true ? self.frame.size.height * CGFloat(index) : index == 0 ? 0 : sizeOfViews - objectView.frame.size.height)
frame.size = fillContent == true ? self.frame.size : objectView.frame.size
frame.origin.x = 0

self.contentSize = CGSize(width: self.frame.size.width,height: fillContent == true ? self.frame.size.height * CGFloat(numberOfViews) : sizeOfViews)
}

view.frame = frame

view.addSubview(objectView)

self.addSubview(view)

addSubview(view)
}

}