monotouch中UIPageControl的使用

本文介绍如何使用UIPageControl实现iOS应用中的手动和自动页面切换功能,通过代码示例展示了如何设置UIScrollView和UIPageControl以实现流畅的幻灯片效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
		}

		//...

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值