Xamarin iOS教程之進度條和滾動檢視
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=值;
其中,值是一個浮點型別的資料,它的有效範圍為0到1。
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Xamarin iOS教程之警告檢視iOS
- Xamarin iOS教程之新增和定製檢視iOS
- Xamarin iOS教程之自定義檢視iOS
- Xamarin iOS教程之檢視顯示影像iOS
- Xamarin.Forms的滾動檢視ScrollViewORMView
- v$session_longops 檢視回滾進度SessionGo
- Xamarin iOS教程之顯示和編輯文字iOS
- 不可思議的純 CSS 滾動進度條效果CSS
- Xamarin XAML語言教程Xamarin.Forms中構建進度條ORM
- Xamarin XAML語言教程使用方法設定進度條進度
- Xamarin XAML語言教程Progress屬性設定進度條進度
- 解決集合檢視的header遮住滾動條Header
- 檢視RMAN備份進度的兩條SQLSQL
- Xamarin iOS教程之鍵盤的使用和設定iOS
- Xamarin XAML語言教程構建進度條ProgressBar
- iOS 自定義進度條iOS
- IOS橫線滾動檢視的實現---方式二iOS
- SGPagingView(iOS強大的標題滾動檢視)ViewiOS
- 拖拽滾動檢視(一)
- vue 自定義指令實現,滾動條百分比進度條。Vue
- iOS WKWebView新增進度條02iOSWebView
- ios自定義圓環進度條iOS
- iOS UIWebview仿微信進度條iOSUIWebView
- Xamarin XAML語言教程使用屬性設定進度條的當前進度
- Xamarin iOS教程之頁面控制元件iOS控制元件
- Xamarin iOS開發實戰上冊----------2.2 新增和定製檢視iOS
- 【新特性速遞】進度條,進度條,進度條
- iOS 怎麼在滾動檢視上懸浮一個ViewiOSView
- javascript如何獲取滾動條的寬度JavaScript
- Xamarin XAML語言教程使用Progress屬性資料繫結設定進度條進度
- 檢視慢查詢進度
- 自己封裝的滾動條滾到底部和可視區域的外掛封裝
- iOS實現音訊進度條效果iOS音訊
- 下拉選單框和滾動條
- Xamarin iOS教程之編輯介面編寫程式碼iOS
- Xamarin XAML語言教程通過ProgressTo方法對進度條設定
- 短視訊app開發,更改進度條的滑動速度APP
- div滾動條樣式,水平滾動