monotouch中UIPageControl的使用

UIPageControl类似web中的幻灯片,手动触发可以切换视图(界面),当然可以用NSTimer来控制自动切换。看代码:

using System;
//
using System.Drawing;
using MonoTouch.UIKit;
using System.Collections.Generic;

namespace GCForum
{
	public class PagingController : UIViewController
	{
		public PagingController ()
		{
		}

		UIScrollView _scroll;
		List<UIView> _pages;
		UIPageControl _pager;

		int _numPages=4; //总共有几页
		float _padding=10;
		float _pageHeight=380;
		float _pageWidth=300;

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();

			View.BackgroundColor = UIColor.Black;
			_pages = new List<UIView> ();

			_scroll = new UIScrollView {
				Frame = this.View.Frame,
				PagingEnabled = true,			
				ContentSize = new SizeF(_numPages * ( _pageWidth + 2 * _padding), this.View.Frame.Height)
			};
			this.View.AddSubview (_scroll);

			for (int i = 0; i < _numPages; i++) {
				UIView v = new UIView ();
				v.Add( new UILabel{
					Frame = new RectangleF (100, 50, 100, 25), 
					Text = String.Format("Page {0}", i+1)}
				);

				_pages.Add (v);
				v.BackgroundColor = UIColor.LightGray;

				v.Frame = new RectangleF (
					i * + _pageWidth + _padding + (2 * _padding * i), 
					0, _pageWidth, _pageHeight);

				_scroll.AddSubview (v);
			}

			_scroll.Scrolled += delegate {
				_pager.CurrentPage = (int)Math.Round (_scroll.ContentOffset.X / _pageWidth);
			};
			_pager = new UIPageControl ();
			_pager.Pages = _numPages;
			_pager.Frame = new RectangleF (0, 420, View.Frame.Width, 50);

			this.View.AddSubview (_pager);
		}

		//...

	}
}
monotouch中UIPageControl的使用_第1张图片

你可能感兴趣的:(uiscrollview,UIPageControl,currentPage)