Skip to content

Instantly share code, notes, and snippets.

@srkirkland
Created December 6, 2014 01:52
Show Gist options
  • Save srkirkland/55258a6ac3adaa13101f to your computer and use it in GitHub Desktop.
Save srkirkland/55258a6ac3adaa13101f to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace pagingnav
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
public class PagingNavController : UIViewController {
UIScrollView scrollView;
UIPageControl pageControl;
UIView navbarView;
UILabel navTitleLabel1;
UILabel navTitleLabel2;
UILabel navTitleLabel3;
UIView view1;
UIView view2;
UIView view3;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
View.BackgroundColor = UIColor.LightGray;
var wBounds = View.Bounds.Width;
var hBounds = View.Bounds.Height;
scrollView = new UIScrollView ();
scrollView.BackgroundColor = UIColor.Clear;
scrollView.Frame = View.Frame;
scrollView.PagingEnabled = true;
scrollView.ShowsHorizontalScrollIndicator = false;
scrollView.Bounces = false;
View.AddSubview (scrollView);
scrollView.ContentSize = new CoreGraphics.CGSize (View.Bounds.Size.Width * 3, hBounds / 2); //3 pages?
//Putting a subview in the navigationbar to hold the titles and page dots
navbarView = new UIView ();
if (NavigationController != null){
NavigationController.NavigationBar.AddSubview (navbarView);
}
//Paging control is added to a subview in the uinavigationcontroller
pageControl = new UIPageControl();
pageControl.Frame = new CoreGraphics.CGRect (0, 35, 0, 0);
pageControl.BackgroundColor = UIColor.White;
pageControl.Pages = 3;
pageControl.CurrentPage = 0;
pageControl.CurrentPageIndicatorTintColor = UIColor.FromRGBA (0.325f, 0.667f, 0.922f, 1f);
pageControl.PageIndicatorTintColor = UIColor.White;
navbarView.AddSubview (pageControl);
//Titles for the nav controller (also added to a subview in the uinavigationcontroller)
//Setting size for the titles. FYI changing width will break the paging fades/movement
var titleSize = new CoreGraphics.CGRect (x: 0, y: 8, width: wBounds, height: 20);
navTitleLabel1 = new UILabel { Frame = titleSize, Text = "Home", TextAlignment = UITextAlignment.Center };
navTitleLabel2 = new UILabel { Frame = titleSize, Text = "Discover", TextAlignment = UITextAlignment.Center };
navTitleLabel3 = new UILabel { Frame = titleSize, Text = "Activity", TextAlignment = UITextAlignment.Center };
navbarView.AddSubview (navTitleLabel1);
navbarView.AddSubview (navTitleLabel2);
navbarView.AddSubview (navTitleLabel3);
view1 = new UIView { BackgroundColor = UIColor.Blue, Frame = new CoreGraphics.CGRect (0, 0, wBounds, hBounds) };
scrollView.AddSubview (view1);
scrollView.BringSubviewToFront (view1);
view2 = new UIView { BackgroundColor = UIColor.Green, Frame = new CoreGraphics.CGRect (wBounds, 0, wBounds, hBounds) };
scrollView.AddSubview (view2);
scrollView.BringSubviewToFront (view2);
view3 = new UIView { BackgroundColor = UIColor.Purple, Frame = new CoreGraphics.CGRect (wBounds * 2, 0, wBounds, hBounds) };
scrollView.AddSubview (view3);
scrollView.BringSubviewToFront (view3);
scrollView.ScrollAnimationEnded += HandleScrollAnimationEnded;
scrollView.Scrolled += HandleScrolled;
scrollView.DecelerationEnded += (sender, e) => {
var xOffset = scrollView.ContentOffset.X;
//Change the pageControl dots depending on the page / offset values
if (xOffset < 1.0) {
pageControl.CurrentPage = 0;
} else if (xOffset < View.Bounds.Width + 1) {
pageControl.CurrentPage = 1;
} else {
pageControl.CurrentPage = 2;
}
Console.WriteLine ("decelerated on page " + pageControl.CurrentPage);
};
}
void HandleScrolled (object sender, EventArgs e)
{
var xOffset = scrollView.ContentOffset.X;
var wBounds = View.Bounds.Width;
var hBounds = View.Bounds.Height;
var widthOffset = wBounds / 100;
var offsetPosition = 0 - xOffset / widthOffset;
//Apply the positioning values created above to the frame's position based on user's scroll
navTitleLabel1.Frame = new CoreGraphics.CGRect (offsetPosition, 8, wBounds, 20);
navTitleLabel2.Frame = new CoreGraphics.CGRect (offsetPosition + 100, 8, wBounds, 20);
navTitleLabel3.Frame = new CoreGraphics.CGRect (offsetPosition + 200, 8, wBounds, 20);
//Change the alpha values of the titles as they are scrolled
navTitleLabel1.Alpha = 1 - xOffset / wBounds;
if (xOffset <= wBounds) {
navTitleLabel2.Alpha = xOffset / wBounds;
} else {
navTitleLabel2.Alpha = 1 - (xOffset - wBounds) / wBounds;
}
navTitleLabel3.Alpha = (xOffset - wBounds) / wBounds;
Console.WriteLine ("Scrollin'");
}
void HandleScrollAnimationEnded (object sender, EventArgs e)
{
Console.WriteLine ("Done scrollin");
}
public override void ViewDidLayoutSubviews ()
{
base.ViewDidLayoutSubviews ();
navbarView.Frame = new CoreGraphics.CGRect (0, 0, View.Bounds.Width, 44);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment