Xamarin iOS教程之進度條和滾動視圖

Xamarin iOS教程之進度條和滾動視圖

Xamarin iOS 進度條

進度條能夠看到每一項任務如今的狀態。例如在下載的應用程序中有進度條,用戶能夠很方便的看到當前程序下載了多少,還剩下多少。QQ音樂播放器中也使用到了進度條,它可讓用戶看到當前音樂播放了多少,還剩多少等。在Xamarin.iOS中也提供實現進度條的類,即UIProgressViewide

【示例2-23】如下將實現進度條加載的效果。具體步驟以下:this

1建立一個Single View Application類型的工程,命名爲2-9spa

2打開MainStoryboard.storyboard文件,對主視圖進行設置。效果如圖2.40所示。線程

2.40  主視圖的效果orm

須要添加的視圖以及設置如表2-12所示。對象

2-12  設置視圖教程

3打開2-9ViewController.cs文件,編寫代碼,實現進度條的加載。代碼以下:事件

  • using System;開發

  • using System.Drawing;rem

  • using MonoTouch.Foundation;

  • using MonoTouch.UIKit;

  • using System.Threading;

  • using System.Threading.Tasks;

  • namespace Application

  • {

  •          public partial class __9ViewController : UIViewController

  •          {

  •                    UIProgressView progressView;

  •                    float incrementBy = 0f;

  •                    ……                                                                   //這裏省略了視圖控制器的構造方法和析構方法

  •                    #region View lifecycle

  •                    public override void ViewDidLoad ()

  •                    {

  •                             base.ViewDidLoad ();

  •                             // Perform any additional setup after loading the view, typically from a nib.

  •               //觸摸按鈕後執行的動做

  •                             buttonStartProgress.TouchUpInside += delegate {

  •                                                buttonStartProgress.Enabled = false;

  •                                                progressView.Progress = 0f;

  •                                                Task.Factory.StartNew(this.StartProgress);                    //建立一個新的任務

  •                             } ;

  •               //爲主視圖添加進度條對象

  •                             progressView = new UIProgressView (new RectangleF (60f, 200f, 200f, 50f));

  •                             progressView.Progress = 0f;                                                 //設置進度條的進度

  •                             incrementBy = 1f / 10f;                                                             //設置進度條進度的增量值

  •                             this.View.AddSubview(progressView);

  •                    }

  •          //進度條開始加載

  •                    public void StartProgress ()

  •                    {

  •                             float currentProgress = 0f;

  •               //判斷currentProgress是否小於1,若是是執行進度條進度的加載

  •                             while (currentProgress < 1f)

  •                             {

  •                                      Thread.Sleep(1000);                                                        //1000毫秒後暫停當前線程

  •                                      this.InvokeOnMainThread(delegate {

  •                                                progressView.Progress += this.incrementBy;

  •                                                currentProgress = this.progressView.Progress;

  •                                                labelStatus.Text=string.Format("Current value: {0}",

  • Math.Round(progressView.Progress,2));

  •                         //判斷進度條的當前進度是否爲1

  •                                                if (currentProgress >= 1f)

  •                                                {

  •                                                         labelStatus.Text = "Progress completed!";

  •                                                         buttonStartProgress.Enabled = true;

  •                                                }

  •                                      } );

  •                             }

  •                    }

  •               ……                                                                  //這裏省略了視圖加載和卸載先後的一些方法

  •                    #endregion

  •          }

  • }

運行效果如圖2.41所示。

2.41  運行效果

在此程序中,開發者須要注意兩個知識點:

1.進度條進度的設置

在實例化進度條時,咱們就爲進度條設置了進度,使用的屬性是Progress。其語法形式以下:

  • 進度條對象.Progress=;

其中,值是一個浮點類型的數據,它的有效範圍爲01

2.進度的增長

當觸摸Tap to start progress!按鈕時,進度條就會實現自動加載進度的功能。它是經過調用Task.Factory.StartNew()方法實現的。它的功能就是建立一個StartProgress()方法的任務,即實現加載。

Xamarin iOS滾動視圖

因爲iPhone或者是iPad屏幕大小的影響,使咱們添加的控件和界面元素受到限制。可是在iPhone或者iPad開發中,人們使用滾動視圖解決了這一受到限制的問題。滾動視圖由UIScrollView類的一個實例對象實現。

【示例2-24】如下的代碼就使用了滾動視圖來顯示一個比屏幕還要大的圖像。具體步驟以下:

1建立一個Single View Application類型的工程,命名爲2-10

2添加圖像1.jpg到建立工程的Resources文件夾中。

3打開2-10ViewController.cs文件,編寫代碼,實現經過滾動視圖來觀看一個比屏幕還有大的圖像。代碼以下:

  • using System;

  • using System.Drawing;

  • using MonoTouch.Foundation;

  • using MonoTouch.UIKit;

  • namespace Application

  • {

  •          public partial class __10ViewController : UIViewController

  •          {

  •                    UIImageView imgView;

  •                    UIScrollView scrollView;

  •               ……                                               //這裏省略了視圖控制器的構造方法和析構方法

  •                    #region View lifecycle

  •                    public override void ViewDidLoad ()

  •                    {

  •                             base.ViewDidLoad ();

  •                             // Perform any additional setup after loading the view, typically from a nib.

  •                             imgView = new UIImageView (UIImage.FromFile ("1.jpg"));

  •               //爲主視圖添加滾動視圖對象

  •                             scrollView = new UIScrollView ();

  •                             scrollView.Frame=new RectangleF(0,0,320,568) ;

  •                             scrollView.ContentSize = imgView.Image.Size;                                  //滾動範圍的大小

  •                             scrollView.ContentOffset = new PointF (200f, 50f);                           //目前滾動的位置

  •                             scrollView.PagingEnabled = true;                                                           //能夠整頁翻動

  •                             scrollView.MinimumZoomScale = 0.25f;                                               //縮小的最小比例

  •                             scrollView.MaximumZoomScale = 2f;                                                   //放大的最大比例

  •               //獲取要縮放的圖像視圖

  •                             scrollView.ViewForZoomingInScrollView = delegate(UIScrollView scroll) {

  •                                      return this.imgView;

  •                             } ;

  •                             scrollView.ZoomScale = 1f;                                                                        //設置變化比例

  •                             scrollView.IndicatorStyle = UIScrollViewIndicatorStyle.Black;             //滾動指示器的風格設置

  •                             scrollView.AddSubview (imgView);

  •                             this.View.AddSubview (scrollView);

  •                    }

  • ……                                               //這裏省略了視圖加載和卸載先後的一些方法

  •                    #endregion

  •          }

  • }

運行結果如圖2.42所示。

2.42  運行效果

注意:滾動視圖中須要注意如下兩點。

1.經常使用屬性

滾動視圖的屬性有不少,表2-13就總結了滾動視圖經常使用的一些屬性。

2-13  滾動視圖的屬性

2.滾動視圖經常使用事件

在滾動視圖中通常會使用到一些事件。這裏將經常使用到的一些事件作了總結,如表2-14所示。

2-14  滾動視圖經常使用事件

【示例2-25】如下將實現滾動視圖的滾動,併爲滾動視圖添加了事件。代碼以下:

  • using System;

  • using System.Drawing;

  • using MonoTouch.Foundation;

  • using MonoTouch.UIKit;

  • namespace Application

  • {

  •          public partial class __30ViewController : UIViewController

  •          {

  •                    ……                                                         //這裏省略了視圖控制器的構造方法和析構方法

  •                    #region View lifecycle

  •                    public override void ViewDidLoad ()

  •                    {

  •                             base.ViewDidLoad ();

  •                             // Perform any additional setup after loading the view, typically from a nib.

  •                             UIScrollView scrollView = new UIScrollView ();

  •                             scrollView.Frame = new RectangleF (0, 0, 320, 568);

  •                             scrollView.ContentSize = new SizeF (320, 2000);

  •                             this.View.AddSubview (scrollView);

  •                             //滾動視圖開始滾動時調用

  •                             scrollView.Scrolled += delegate {

  •                                      Console.WriteLine ("開始滾動...");

  •                             } ;

  •                             //滾動視圖結束滾動時調用

  •                             scrollView.DecelerationEnded += delegate {

  •                                      Console.WriteLine ("滾動結束...");

  •                             };

  •                             float y = 10;

  •                             //爲滾動視圖對象添加標籤對象

  •                             for (float i = 1; i < 21; i++) {

  •                                      UILabel label = new UILabel ();

  •                                      label.Frame = new RectangleF (0, y, 320, 50);

  •                                      label.BackgroundColor = UIColor.Cyan;

  •                                      label.Text = String.Format ("{0}", i);

  •                                      scrollView.AddSubview (label);

  •                                      y += 100;

  •                             }

  •                    }

  • ……                                                 //這裏省略了視圖加載和卸載先後的一些方法

  •                    #endregion

  •          }

  • }

運行效果如圖2.43所示。

2.43  運行效果

本文選自:Xamarin iOS開發實戰大學霸內部資料,轉載請註明出處,尊重技術尊重IT人!

相關文章
相關標籤/搜索