Skip to content

Commit

Permalink
fix memory leak issue
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckDeck authored and DuckDeck committed Jul 9, 2016
1 parent bddbef0 commit 1ede5eb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
4 changes: 2 additions & 2 deletions GrandMenu.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "GrandMenu"
s.version = "0.0.4"
s.version = "0.0.5"
s.summary = "GrandMenu is a powerful and simple Menu"

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -80,7 +80,7 @@ Pod::Spec.new do |s|
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/DuckDeck/GrandMenu.git", :tag => "0.0.4" }
s.source = { :git => "https://github.com/DuckDeck/GrandMenu.git", :tag => "0.0.5" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
75 changes: 39 additions & 36 deletions GrandMenu/GrandMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
import UIKit

public class GrandMenu: UIView,GraneMenuItemDelegate {
public var arrItemsTitle:[String]?{
public var arrItemsTitle:[String]?{
didSet{
setupItems()
}
}
public var sliderBarLeftRightOffset = 15{
public var sliderBarLeftRightOffset = 15{
didSet{
setupItems()
}
}
public var sliderBarHeight = 2{
public var sliderBarHeight = 2{
didSet{
setupItems()
}
}
public var sliderColor = UIColor.redColor(){
public var sliderColor = UIColor.redColor(){
didSet{
vSlider?.backgroundColor = sliderColor
}
Expand All @@ -37,49 +37,49 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
}
}
}
public var defaultSelectedIndex:Int = 0{
public var defaultSelectedIndex:Int = 0{
didSet{
if defaultSelectedIndex < arrItemsTitle!.count{
setupItems()
}
}
}

public var itemColor:UIColor = UIColor.blackColor(){
public var itemColor:UIColor = UIColor.blackColor(){
didSet{
if let items = arrItems{
for item in items{
item.color = itemColor
}
}

}
}

public var itemSeletedColor:UIColor = UIColor.redColor(){
public var itemSeletedColor:UIColor = UIColor.redColor(){
didSet{
if let items = arrItems{
for item in items{
item.selectedColor = itemSeletedColor
item.selectedColor = itemSeletedColor
}
}
}
}

public var itemFont:Float?{
public var itemFont:Float?{
didSet{
if itemFont == nil{
return
}
if let items = arrItems{
for item in items{
item.fontSize = itemFont!
item.fontSize = itemFont!
}
}
}
}

public var itemSelectedFont:Float?{
public var itemSelectedFont:Float?{
didSet{
if itemSelectedFont == nil{
return
Expand Down Expand Up @@ -111,7 +111,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
private override init(frame: CGRect) {
super.init(frame: frame)
arrItems = [GrandMenuItem]()
scrollView = UIScrollView(frame: self.bounds)
scrollView = UIScrollView(frame: bounds)
scrollView!.backgroundColor = UIColor.clearColor()
scrollView!.showsHorizontalScrollIndicator = false
scrollView!.showsVerticalScrollIndicator = false
Expand All @@ -121,7 +121,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
vSlider?.backgroundColor = sliderColor
scrollView!.addSubview(vSlider!)
}
public convenience init(frame: CGRect,titles:[String]){
public convenience init(frame: CGRect,titles:[String]){
self.init(frame:frame)
if let window = UIApplication.sharedApplication().keyWindow
{
Expand All @@ -139,13 +139,14 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
setupItems()
}
public func addBottomLine(bgColor:UIColor,size:CGSize){
vBottomLine = UIView(frame: CGRect(x: (self.frame.size.width - size.width) / 2, y: self.frame.size.height, width: size.width, height: size.height))
vBottomLine = UIView(frame: CGRect(x: (frame.size.width - size.width) / 2, y: frame.size.height, width: size.width, height: size.height))
vBottomLine?.backgroundColor = bgColor
addSubview(vBottomLine!)
self.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: CGRectGetMaxY(vBottomLine!.frame))
frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: CGRectGetMaxY(vBottomLine!.frame))
}

func setupItems(){

func setupItems(){
weak var weakSelf = self
for view in scrollView!.subviews{
if view is GrandMenuItem{
view.removeFromSuperview()
Expand All @@ -163,7 +164,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
if let sf = itemSelectedFont{
item.selectedFontSize = sf
}
item.delegate = self
item.delegate = weakSelf
var itemWidth = GrandMenuItem.getTitleWidth(title,fontSize: item.fontSize)
if averageManu{
itemWidth = Float(UIScreen.mainScreen().bounds.width / CGFloat(arrItemsTitle!.count))
Expand All @@ -184,7 +185,8 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {


func scrollToVisibleItem(item:GrandMenuItem){
let selectedItemIndex = arrItems!.find({(s:GrandMenuItem) ->Bool in return s.title! == self.selectedItem!.title!}).1
weak var weakSelf = self
let selectedItemIndex = arrItems!.find({(s:GrandMenuItem) ->Bool in return s.title! == weakSelf?.selectedItem!.title!}).1
let visibleItemIndex = arrItems!.find({(s:GrandMenuItem) ->Bool in return s.title! == item.title!}).1
if selectedItemIndex == visibleItemIndex{
return
Expand Down Expand Up @@ -237,12 +239,13 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {
// vSlider?.layer.bounds = rect
//这个动画有个小Bug,目前不知道怎么修正,它会先把滑块移动到目的地再消失,再执行动画,所以只好用下面的方法了,这种情况有30%的可能性发生
//不用这种动画试试
weak var weakSelf = self
UIView.animateWithDuration(0.2) { () -> Void in
self.vSlider?.frame = CGRect(x: self.vSlider!.frame.origin.x + dx, y: self.vSlider!.frame.origin.y, width: self.vSlider!.frame.size.width, height: self.vSlider!.frame.size.height)
weakSelf?.vSlider?.frame = CGRect(x: weakSelf!.vSlider!.frame.origin.x + dx, y:weakSelf!.vSlider!.frame.origin.y, width: weakSelf!.vSlider!.frame.size.width, height: weakSelf!.vSlider!.frame.size.height)
}
}

func adjustScrollOffset(){
public func adjustScrollOffset(){
let x = selectedItem!.frame.origin.x
// if x < selectedItem!.frame.size.width * 2 || x > scrollView!.contentSize.width - 2 * selectedItem!.frame.size.width {
// return
Expand Down Expand Up @@ -274,7 +277,7 @@ public class GrandMenu: UIView,GraneMenuItemDelegate {

}

public func selectSlideBarItemAtIndex(index:Int){
public func selectSlideBarItemAtIndex(index:Int){
let item = arrItems![index]
if item == selectedItem!{
return
Expand Down Expand Up @@ -302,17 +305,17 @@ extension Array{
mutating func remove(method:(Element)->Bool)->(Element?,Bool){
let result = find(method)
if result.1 >= 0{
return (self.removeAtIndex(result.1),true)
return (removeAtIndex(result.1),true)
}
return (nil,false)
}

func compareFirstObject(arrTarget:Array, method:(Element,Element)->Bool)->Bool{
if self.count == 0 || arrTarget.count == 0
if count == 0 || arrTarget.count == 0
{
return false
}
let firstItem = self.first!
let firstItem = first!
let targetFirstItem = arrTarget.first!
if method(firstItem,targetFirstItem)
{
Expand All @@ -322,11 +325,11 @@ extension Array{
}

func compareLastObject(arrTarget:Array, method:(Element,Element)->Bool)->Bool{
if self.count == 0 || arrTarget.count == 0
if count == 0 || arrTarget.count == 0
{
return false
}
let firstItem = self.last!
let firstItem = last!
let targetFirstItem = arrTarget.last!
if method(firstItem,targetFirstItem)
{
Expand All @@ -337,30 +340,30 @@ extension Array{

mutating func exchangeObjectAdIndex(IndexA:Int,atIndexB:Int)
{
if IndexA >= self.count || IndexA < 0{
if IndexA >= count || IndexA < 0{
return
}
if atIndexB >= self.count || atIndexB < 0{
if atIndexB >= count || atIndexB < 0{
return
}
let objA = self[IndexA]
let objB = self[atIndexB]
self.replaceObject(objA, atIndex: atIndexB)
self.replaceObject(objB, atIndex: IndexA)
replaceObject(objA, atIndex: atIndexB)
replaceObject(objB, atIndex: IndexA)
}

mutating func replaceObject(obj:Element,atIndex:Int){
if atIndex >= self.count || atIndex < 0{
if atIndex >= count || atIndex < 0{
return
}
self.removeAtIndex(atIndex)
self.insert(obj, atIndex: atIndex)
removeAtIndex(atIndex)
insert(obj, atIndex: atIndex)
}

mutating func merge(newArray:Array){
for obj in newArray
{
self.append(obj)
append(obj)
}
}
}
4 changes: 2 additions & 2 deletions GrandMenu/GrandMenuItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import UIKit
protocol GraneMenuItemDelegate{
protocol GraneMenuItemDelegate:NSObjectProtocol{
func GraneMenuItemSelected(item:GrandMenuItem)
}
public class GrandMenuItem: UIView {
Expand Down Expand Up @@ -44,7 +44,7 @@ public class GrandMenuItem: UIView {
}
}

var delegate:GraneMenuItemDelegate?
weak var delegate:GraneMenuItemDelegate?

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Expand Down
7 changes: 4 additions & 3 deletions GrandMenu/GrandMenuTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ public class GrandMenuTable: UIView,UITableViewDataSource,UITableViewDelegate {
}

public func selectIndex(index:Int){
UIView.animateWithDuration(0.3) { () -> Void in
self.tb?.scrollToRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0), atScrollPosition: .None, animated: false)
}
weak var weakSelf = self
UIView.animateWithDuration(0.3) { () -> Void in
weakSelf?.tb?.scrollToRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0), atScrollPosition: .None, animated: true)
}
}

}

0 comments on commit 1ede5eb

Please sign in to comment.