您的位置:首页 > 产品设计 > UI/UE

monotouch中UIPageControl的使用

2013-12-09 16:24 260 查看
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);
		}

		//...

	}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: