自定义UIActivityIndicatorView加载效果及具体应用

代码:

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

namespace xxxx
{
	public class LoadingOverlay : UIView
	{
		// control declarations
		UIActivityIndicatorView activitySpinner;
		UILabel loadingLabel;

		public LoadingOverlay (RectangleF frame) : base (frame)
		{
			// configurable bits
			BackgroundColor = UIColor.Black;
			Alpha = 0.75f;
			AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;

			float labelHeight = 22;
			float labelWidth = Frame.Width - 20;

			// derive the center x and y
			float centerX = Frame.Width / 2;
			float centerY = Frame.Height / 2;

			// create the activity spinner, center it horizontall and put it 5 points above center x
			activitySpinner = new UIActivityIndicatorView (UIActivityIndicatorViewStyle.WhiteLarge);
			activitySpinner.Frame = new RectangleF (
				centerX - (activitySpinner.Frame.Width / 2),
				centerY - activitySpinner.Frame.Height - 20,
				activitySpinner.Frame.Width,
				activitySpinner.Frame.Height);
			activitySpinner.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
			AddSubview (activitySpinner);
			activitySpinner.StartAnimating ();

			// create and configure the "Loading Data" label
			loadingLabel = new UILabel (new RectangleF (
				centerX - (labelWidth / 2),
				centerY + 20,
				labelWidth,
				labelHeight
			));
			loadingLabel.BackgroundColor = UIColor.Clear;
			loadingLabel.TextColor = UIColor.White;
			loadingLabel.Text = "Loading Data...";
			loadingLabel.TextAlignment = UITextAlignment.Center;
			loadingLabel.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;
			AddSubview (loadingLabel);
		}

		/// <summary>
		/// Fades out the control and then removes it from the super view
		/// </summary>
		public void Hide ()
		{
			UIView.Animate (
				0.5, // duration
				() => { Alpha = 0; },
				() => { RemoveFromSuperview (); }
			);
		}

		//...

	}
}

调用:

/// <summary>
		/// 刷新
		/// </summary>
		private void RefashBtnClick(object sender, System.EventArgs e)
		{
			//show loading...
			xxxx.LoadingOverlay loadingOverlay = new xxxx.LoadingOverlay (this.View.Bounds);
			this.View.AddSubview (loadingOverlay);

			NSUserDefaults.StandardUserDefaults.RemoveObject (KeyCenter.Key_NewsList); //移除缓存
			ListAT = ArticleBusiness.GetData ();
			if (ListAT == null) {
				Tools.Alert ("数据获取失败!");	
				return;
			}
			//*=================================>test
			ListAT.Add (new ArticleModel () {
					Articleid="132",
					Articletitle = "i just test it for show",
					ArticleContent = "http://xxxxxxxxx/article/padarticle.aspx?id=xxx",
					Articlechannel = "80",
					Articlecreatedate="xxxxx",
					Articleimg="",
					ArticleAuthor="jon",
					Outline="jonjonjonjonjonjonjon"
			});

			DownLoadFile.BindTableView (this, TBView, ListAT, KeyCenter.Key_LoadedImgPath);

			//hide loading...
			loadingOverlay.Hide ();
		}


结合上一篇文章,优化显示处理:

using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace GCForum
{
	/// <summary>
	/// 详细页面
	/// </summary>
	public partial class NewsDetail : BaseViewBodyController
	{
		private UIButton RefashBtn;
		private string ArticleURL;
		private xxxxx.LoadingOverlay loadingOverlay;

		public NewsDetail () : base ("NewsDetail", null)
		{
		}

		/// <summary>
		/// 初始化详情页面
		/// </summary>
		/// <param name="url">url地址.</param>
		public NewsDetail(string url)
		{
			this.ArticleURL = url;
			this.ThisViewTitle = "前沿资讯";
		}

		public override void ViewWillAppear (bool animated)
		{
			base.ViewWillAppear (animated);

			//刷新
			RefashBtn = chongqing.ControlCenter.RefreshButton ("");
			RefashBtn.TouchUpInside += RefashBtnClick;
			this.NavigationItem.RightBarButtonItem = new UIBarButtonItem (RefashBtn);
		}

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

		/// <summary>
		/// 刷新
		/// </summary>
		private void RefashBtnClick(object sender, System.EventArgs e)
		{
			ShowNewsInfo ();
		}

		private void ShowNewsInfo()
		{
			loadingOverlay = new xxxxx.LoadingOverlay (this.View.Bounds);
			this.View.AddSubview (loadingOverlay);
			System.Threading.Thread t = new System.Threading.Thread (GetNewsInfo);
			t.Start ();
		}
		void GetNewsInfo()
		{
			System.Threading.Thread.Sleep (500);
			using (var pool=new NSAutoreleasePool()) {
				this.InvokeOnMainThread (delegate {
					RectangleF rec = this.View.Bounds;
					using (var webView = new UIWebView(new RectangleF(rec.Left,rec.Top,rec.Width,rec.Height-5))) {
						//load data
						var urlRequest = new NSUrlRequest (new NSUrl (ArticleURL));
						webView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
						webView.BackgroundColor = UIColor.White;
						webView.LoadRequest (urlRequest);
						this.View.Add (webView);
					}

					//hide loading...
					loadingOverlay.Hide ();
				});
			}
		}

		//...

	}
}

webveiw具体的隐藏还是应该在loadfinished事件中处理比较好:

webView.LoadFinished += delegate {
	if (loadingOverlay != null) {
		loadingOverlay.Hide ();
		loadingOverlay.Dispose ();
		loadingOverlay = null;
	}
};




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值