From dce68131c7d994aaeb7091aca86e497e43012a33 Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 26 Jan 2016 16:18:33 +0300 Subject: [PATCH] no message --- Display/CAAnimationUtils.swift | 13 ++-- Display/CALayer+ImplicitAnimations.m | 4 +- Display/NavigationBar.swift | 9 ++- Display/TabBarContollerNode.swift | 4 +- Display/TabBarController.swift | 16 ++++- Display/TabBarNode.swift | 92 ++++++++++++++++++++++++---- 6 files changed, 115 insertions(+), 23 deletions(-) diff --git a/Display/CAAnimationUtils.swift b/Display/CAAnimationUtils.swift index d7232bd9db..61d643ceda 100644 --- a/Display/CAAnimationUtils.swift +++ b/Display/CAAnimationUtils.swift @@ -37,7 +37,7 @@ public extension CAAnimation { } public extension CALayer { - public func animate(from from: NSValue, to: NSValue, keyPath: String, timingFunction: String, duration: NSTimeInterval, completion: (Bool -> Void)? = nil) { + public func animate(from from: NSValue, to: NSValue, keyPath: String, timingFunction: String, duration: NSTimeInterval, additive: Bool, completion: (Bool -> Void)? = nil) { let k = Float(UIView.animationDurationFactor()) var speed: Float = 1.0 if k != 0 && k != 1 { @@ -52,6 +52,7 @@ public extension CALayer { animation.removedOnCompletion = true animation.fillMode = kCAFillModeForwards animation.speed = speed + animation.additive = additive if let completion = completion { animation.delegate = CALayerAnimationDelegate(completion: completion) } @@ -61,11 +62,15 @@ public extension CALayer { //self.setValue(to, forKey: keyPath) } - public func animateAlpha(from from: CGFloat, to: CGFloat, duration: NSTimeInterval) { - self.animate(from: NSNumber(float: Float(from)), to: NSNumber(float: Float(to)), keyPath: "opacity", timingFunction: kCAMediaTimingFunctionEaseInEaseOut, duration: duration) + public func animateAlpha(from from: CGFloat, to: CGFloat, duration: NSTimeInterval, completion: ((Bool) -> ())? = nil) { + self.animate(from: NSNumber(float: Float(from)), to: NSNumber(float: Float(to)), keyPath: "opacity", timingFunction: kCAMediaTimingFunctionEaseInEaseOut, duration: duration, additive: false, completion: completion) } internal func animatePosition(from from: CGPoint, to: CGPoint, duration: NSTimeInterval) { - self.animate(from: NSValue(CGPoint: from), to: NSValue(CGPoint: to), keyPath: "position", timingFunction: kCAMediaTimingFunctionEaseInEaseOut, duration: duration) + self.animate(from: NSValue(CGPoint: from), to: NSValue(CGPoint: to), keyPath: "position", timingFunction: kCAMediaTimingFunctionEaseInEaseOut, duration: duration, additive: false) + } + + public func animateBoundsOriginYAdditive(from from: CGFloat, to: CGFloat, duration: NSTimeInterval) { + self.animate(from: from as NSNumber, to: to as NSNumber, keyPath: "bounds.origin.y", timingFunction: kCAMediaTimingFunctionEaseInEaseOut, duration: duration, additive: true) } } diff --git a/Display/CALayer+ImplicitAnimations.m b/Display/CALayer+ImplicitAnimations.m index 2229d1694f..ca7ec863e2 100644 --- a/Display/CALayer+ImplicitAnimations.m +++ b/Display/CALayer+ImplicitAnimations.m @@ -115,8 +115,8 @@ static NSMutableArray *currentLayerAnimations() static dispatch_once_t onceToken; dispatch_once(&onceToken, ^ { - [RuntimeUtils swizzleInstanceMethodOfClass:[CALayer class] currentSelector:@selector(setBounds:) newSelector:@selector(_ca836a62_setBounds:)]; - [RuntimeUtils swizzleInstanceMethodOfClass:[CALayer class] currentSelector:@selector(setPosition:) newSelector:@selector(_ca836a62_setPosition:)]; + //[RuntimeUtils swizzleInstanceMethodOfClass:[CALayer class] currentSelector:@selector(setBounds:) newSelector:@selector(_ca836a62_setBounds:)]; + //[RuntimeUtils swizzleInstanceMethodOfClass:[CALayer class] currentSelector:@selector(setPosition:) newSelector:@selector(_ca836a62_setPosition:)]; }); } diff --git a/Display/NavigationBar.swift b/Display/NavigationBar.swift index 00552b5332..faf10e441d 100644 --- a/Display/NavigationBar.swift +++ b/Display/NavigationBar.swift @@ -16,6 +16,8 @@ public class NavigationBar: ASDisplayNode { private let stripeHeight: CGFloat = 1.0 / UIScreen.mainScreen().scale + //private let effectView: UIVisualEffectView + var backPressed: () -> () = { } private var collapsed: Bool { @@ -58,9 +60,12 @@ public class NavigationBar: ASDisplayNode { stripeView = UIView() stripeView.backgroundColor = UIColor(red: 0.6953125, green: 0.6953125, blue: 0.6953125, alpha: 1.0) + //self.effectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) + super.init() self.backgroundColor = UIColor(red: 0.968626451, green: 0.968626451, blue: 0.968626451, alpha: 1.0) + //self.view.addSubview(self.effectView) self.view.addSubview(stripeView) } @@ -115,9 +120,11 @@ public class NavigationBar: ASDisplayNode { public override func layout() { - self.stripeView.frame = CGRect(x: 0.0, y: self.frame.size.height - stripeHeight, width: self.frame.size.width, height: stripeHeight) + self.stripeView.frame = CGRect(x: 0.0, y: self.frame.size.height, width: self.frame.size.width, height: stripeHeight) self.topItemWrapper?.layoutItems() self.tempItemWrapper?.layoutItems() + + //self.effectView.frame = self.bounds } } diff --git a/Display/TabBarContollerNode.swift b/Display/TabBarContollerNode.swift index 336a063fe8..ada54e63cf 100644 --- a/Display/TabBarContollerNode.swift +++ b/Display/TabBarContollerNode.swift @@ -14,8 +14,8 @@ class TabBarControllerNode: ASDisplayNode { } } - override init() { - self.tabBarNode = TabBarNode() + init(itemSelected: Int -> Void) { + self.tabBarNode = TabBarNode(itemSelected: itemSelected) super.init() diff --git a/Display/TabBarController.swift b/Display/TabBarController.swift index 0afe63998f..e587d41674 100644 --- a/Display/TabBarController.swift +++ b/Display/TabBarController.swift @@ -45,7 +45,11 @@ public class TabBarController: ViewController { } override public func loadDisplayNode() { - self.displayNode = TabBarControllerNode() + self.displayNode = TabBarControllerNode(itemSelected: { [weak self] index in + if let strongSelf = self { + strongSelf.selectedIndex = index + } + }) self.updateSelectedIndex() } @@ -55,6 +59,8 @@ public class TabBarController: ViewController { return } + self.tabBarControllerNode.tabBarNode.selectedIndex = self.selectedIndex + if let currentController = self.currentController { currentController.willMoveToParentViewController(nil) self.tabBarControllerNode.currentControllerView = nil @@ -78,6 +84,14 @@ public class TabBarController: ViewController { self.tabBarControllerNode.currentControllerView = currentController.view self.addChildViewController(currentController) currentController.didMoveToParentViewController(self) + + self.navigationItem.title = currentController.navigationItem.title + self.navigationItem.leftBarButtonItem = currentController.navigationItem.leftBarButtonItem + self.navigationItem.rightBarButtonItem = currentController.navigationItem.rightBarButtonItem + } else { + self.navigationItem.title = nil + self.navigationItem.leftBarButtonItem = nil + self.navigationItem.rightBarButtonItem = nil } } diff --git a/Display/TabBarNode.swift b/Display/TabBarNode.swift index 93031028ab..0e7277ba4d 100644 --- a/Display/TabBarNode.swift +++ b/Display/TabBarNode.swift @@ -22,13 +22,19 @@ private func tabBarItemImage(image: UIImage?, title: String, tintColor: UIColor) CGContextSetFillColorWithColor(context, UIColor(0xf7f7f7).CGColor) CGContextFillRect(context, CGRect(origin: CGPoint(), size: size)) - image?.drawAtPoint(CGPoint(x: floorToScreenPixels((size.width - imageSize.width) / 2.0), y: 0.0)) + if let image = image { + let imageRect = CGRect(origin: CGPoint(x: floorToScreenPixels((size.width - imageSize.width) / 2.0), y: 0.0), size: imageSize) + CGContextSaveGState(context) + CGContextTranslateCTM(context, imageRect.midX, imageRect.midY) + CGContextScaleCTM(context, 1.0, -1.0) + CGContextTranslateCTM(context, -imageRect.midX, -imageRect.midY) + CGContextClipToMask(context, imageRect, image.CGImage) + CGContextSetFillColorWithColor(context, tintColor.CGColor) + CGContextFillRect(context, imageRect) + CGContextRestoreGState(context) + } - (title as NSString).drawAtPoint(CGPoint(x: floorToScreenPixels((size.width - titleSize.width) / 2.0), y: size.height - titleSize.height - 3.0), withAttributes: [NSFontAttributeName: font]) - - CGContextSetBlendMode(context, .SourceIn) - CGContextSetFillColorWithColor(context, tintColor.CGColor) - //CGContextFillRect(context, CGRect(origin: CGPoint(), size: size)) + (title as NSString).drawAtPoint(CGPoint(x: floorToScreenPixels((size.width - titleSize.width) / 2.0), y: size.height - titleSize.height - 3.0), withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: tintColor]) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() @@ -37,17 +43,34 @@ private func tabBarItemImage(image: UIImage?, title: String, tintColor: UIColor) } class TabBarNode: ASDisplayNode { - let separatorNode: ASDisplayNode - - var tabBarNodes: [ASImageNode] = [] - var tabBarItems: [UITabBarItem] = [] { didSet { self.reloadTabBarItems() } } - override init() { + var selectedIndex: Int? { + didSet { + if self.selectedIndex != oldValue { + if let oldValue = oldValue { + self.updateNodeImage(oldValue) + } + + if let selectedIndex = self.selectedIndex { + self.updateNodeImage(selectedIndex) + } + } + } + } + + private let itemSelected: Int -> Void + + let separatorNode: ASDisplayNode + private var tabBarNodes: [ASImageNode] = [] + + init(itemSelected: Int -> Void) { + self.itemSelected = itemSelected + self.separatorNode = ASDisplayNode() self.separatorNode.backgroundColor = UIColor(0xb2b2b2) self.separatorNode.opaque = true @@ -67,12 +90,17 @@ class TabBarNode: ASDisplayNode { } var tabBarNodes: [ASImageNode] = [] - for item in self.tabBarItems { + for i in 0 ..< self.tabBarItems.count { + let item = self.tabBarItems[i] let node = ASImageNode() node.displaysAsynchronously = false node.displayWithoutProcessing = true node.layerBacked = true - node.image = tabBarItemImage(item.image, title: item.title ?? "", tintColor: UIColor.blueColor()) + if let selectedIndex = self.selectedIndex where selectedIndex == i { + node.image = tabBarItemImage(item.selectedImage, title: item.title ?? "", tintColor: UIColor.blueColor()) + } else { + node.image = tabBarItemImage(item.image, title: item.title ?? "", tintColor: UIColor(0x929292)) + } tabBarNodes.append(node) self.addSubnode(node) } @@ -82,6 +110,19 @@ class TabBarNode: ASDisplayNode { self.setNeedsLayout() } + private func updateNodeImage(index: Int) { + if index < self.tabBarNodes.count && index < self.tabBarItems.count { + let node = self.tabBarNodes[index] + let item = self.tabBarItems[index] + + if let selectedIndex = self.selectedIndex where selectedIndex == index { + node.image = tabBarItemImage(item.selectedImage, title: item.title ?? "", tintColor: UIColor.blueColor()) + } else { + node.image = tabBarItemImage(item.image, title: item.title ?? "", tintColor: UIColor(0x929292)) + } + } + } + override func layout() { super.layout() @@ -103,4 +144,29 @@ class TabBarNode: ASDisplayNode { } } } + + override func touchesBegan(touches: Set!, withEvent event: UIEvent!) { + super.touchesBegan(touches, withEvent: event) + + if let touch = touches.first as? UITouch { + let location = touch.locationInView(self.view) + var closestNode: (Int, CGFloat)? + + for i in 0 ..< self.tabBarNodes.count { + let node = self.tabBarNodes[i] + let distance = abs(location.x - node.position.x) + if let previousClosestNode = closestNode { + if previousClosestNode.1 > distance { + closestNode = (i, distance) + } + } else { + closestNode = (i, distance) + } + } + + if let closestNode = closestNode { + self.itemSelected(closestNode.0) + } + } + } } \ No newline at end of file