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

大學霸發表於2015-06-19

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

Xamarin iOS 進度條

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

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

1建立一個Single View Application型別的工程,命名為2-9

2開啟MainStoryboard.storyboard檔案,對主檢視進行設定。效果如圖2.40所示。


2.40  主檢視的效果

需要新增的檢視以及設定如表2-12所示。

2-12  設定檢視


3開啟2-9ViewController.cs檔案,編寫程式碼,實現進度條的載入。程式碼如下:


  • using System;
  • using System.Drawing;
  • 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人!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29597077/viewspace-1705437/,如需轉載,請註明出處,否則將追究法律責任。

相關文章