Xamarin.ios 基本控件

  1// 1、按鈕 UIButton
  2                            UIButton btn = new UIButton();
  3             btn.Frame = new RectangleF(150,310,80,30); //按鈕位置一件寬高
  4             btn.SetTitle("Button",UIControlState.Normal); //顯示的文字
  5             btn.SetTitleColor(UIColor.Black,UIControlState.Normal);//文字的顏色
  6             btn.TouchUpInside += this.SetBackGroudColor; //按鈕 觸摸事件
  7             this.View.AddSubview(btn); //添加到當前視圖
  8 
  9 //2、圖像視圖 UIImageView
 10             UIImageView img=new UIImageView();
 11             img.Frame = new RectangleF(0,0,375,667); //設置位置大小
 12             img.ContentMode = UIViewContentMode.ScaleAspectFit; //設置圖像視圖模式
 13             img.Image = UIImage.FromFile("img.jpg"); //設置圖像 
 14             img.Transform = CGAffineTransform.MakeRotation(20);//圖像旋轉正數順時針旋轉,負數逆時針旋轉  
 15             img.Transform = CGAffineTransform.MakeScale(-2,2);  //圖像縮放
 16             this.View.AddSubview(img); //添加到當前視圖   
 17 
 18 //3、label控件 UILabel 
 19             UILabel lbl1 = new UILabel();
 20             lbl1.Frame = new RectangleF(2,410,155,28);
 21             lbl1.Text = "UILabel";
 22             lbl1.Lines = 3; //顯示行數 配合   label高度 使用
 23             lbl1.LineBreakMode = UILineBreakMode.MiddleTruncation; //內容顯示樣式 
 24             lbl1.Hidden = true;  //是否顯示
 25             this.View.AddSubview(lbl1);  
 26 
 27 //4、輸入框 UITextField
 28             UITextField  t1 = new UITextField();
 29             t1.BorderStyle = UITextBorderStyle.RoundedRect;
 30             t1.Frame = new RectangleF(50,250,300,30);
 31             t1.Placeholder = "帳號";
 32             //  限制輸入字符長度
 33             t1.ShouldChangeCharacters = (textField, range, replacementString) => {
 34                 if (range.Location < 10)
 35                     return true;
 36                 else
 37                     return false;
 38             };
 39             this.View.AddSubview(t1);
 40             UITextField t2 = new UITextField(); 
 41             t2.BorderStyle = UITextBorderStyle.RoundedRect;//邊框樣式
 42             t2.Frame = new RectangleF(50, 300, 300, 30);
 43             t2.Placeholder = "密碼";
 44             t2.SecureTextEntry = true; //隱藏文本
 45             this.View.AddSubview(t2);  
 46 
 47 
 48             UITextView text = new UITextView();
 49             text.Frame = new RectangleF(9, 90, 302, 180);
 50 
 51             this.View.AddSubview(text);
 52 //5、UITextView 實現功能:點擊完成按鈕隱藏鍵盤,而且把text的Text值賦值給text1控件,並監控 text控件的狀態(開始輸入,結束輸入,輸入中)
 53             UITextView text1 = new UITextView();
 54             text1.Frame = new RectangleF(9, 330, 302, 180);
 55             text1.Editable = false;
 56             this.View.AddSubview(text1);
 57             text1.Hidden = true;
 58             UIButton btn = new UIButton();
 59             btn.Frame = new RectangleF(137,56,46,30);
 60             btn.SetTitle("完成", UIControlState.Normal);
 61             btn.SetTitleColor(UIColor.Red,UIControlState.Normal);
 62             this.View.AddSubview(btn);
 63             btn.TouchUpInside += (sender, e) => {
 64                 text.ResignFirstResponder();
 65                 text1.Hidden=false;
 66                 text1.Text = text.Text;
 67 
 68             };
 69             text.Delegate = new MyDelegate();
 70 
 71         }
 72         private class MyDelegate : UITextViewDelegate
 73         {
 74             public override void EditingEnded(UITextView textView)
 75             {
 76                 Console.WriteLine("Edit end");
 77             }
 78             public override void EditingStarted(UITextView textView)
 79             {
 80                 Console.WriteLine("Edit start");
 81             }
 82             public override void Changed(UITextView textView)
 83             {
 84                 Console.WriteLine("Edit ");
 85             }
 86         }  
 87 //6、 輸入框鍵盤 樣式類型 設置
 88             UITextField fied = new UITextField();
 89             fied.Frame = new RectangleF(50, 100, 300, 30);
 90             fied.BorderStyle = UITextBorderStyle.Line; 
 91             this.View.AddSubview(fied);  
 92             fied.KeyboardType = UIKeyboardType.Url;//設置鍵盤類型
 93             fied.KeyboardAppearance = UIKeyboardAppearance.Dark;// 設置鍵盤外觀
 94             fied.ReturnKeyType = UIReturnKeyType.Next;//設置鍵盤的Return鍵  
 95 
 96// 7、顯示鍵盤 時改變輸入框的位置
 97                  UITextField fied = new UITextField();
 98         private NSObject kbdWillShow, kbdDidHide;
 99 
100         public override void ViewDidLoad()
101         {
102             base.ViewDidLoad(); 
103             fied.Frame = new RectangleF(10, 500, 300, 30);
104             fied.BorderStyle = UITextBorderStyle.RoundedRect; 
105              this.View.AddSubview(fied);
106             //顯示鍵盤前 (事件)
107             kbdWillShow = UIKeyboard.Notifications.ObserveWillShow((s, e) => {
108                 RectangleF kbdRounds = (RectangleF)e.FrameEnd;
109                 RectangleF textFrame = (RectangleF)fied.Frame;
110                 textFrame.Y -= kbdRounds.Height;
111                 fied.Frame = textFrame;
112             });
113             //隱藏鍵盤後  (事件)
114             kbdDidHide = UIKeyboard.Notifications.ObserveDidHide((s, e) =>
115             {
116                 RectangleF kbdRounds = (RectangleF)e.FrameEnd;
117                 RectangleF textFrame = (RectangleF)fied.Frame;
118                 textFrame.Y += kbdRounds.Height;
119                 fied.Frame = textFrame;
120             });
121             //鍵盤上的return鍵 隱藏鍵盤 (事件)
122             fied.ShouldReturn=delegate(UITextField textField) {
123                 return textField.ResignFirstResponder();
124             };
125         }
126  //8、 爲輸入框鍵盤 添加工具欄
127             UITextField fied = new UITextField();
128             fied.Frame = new RectangleF(10, 200, 300, 30);
129             fied.BorderStyle = UITextBorderStyle.RoundedRect;//設置輸入框邊框
130              this.View.AddSubview(fied);
131             fied.KeyboardType = UIKeyboardType.EmailAddress;//設置鍵盤類型
132             //設施工具欄
133             UIToolbar bar = new UIToolbar();//實例化工具欄
134             bar.SizeToFit();
135             bar.BackgroundColor = UIColor.DarkGray;//設置背景
136             //實例化 工具欄 按鈕
137             UIBarButtonItem item = new UIBarButtonItem("完成", UIBarButtonItemStyle.Done, (s, e) =>
138             {
139                 //隱藏鍵盤
140                 fied.ResignFirstResponder();
141             });
142             //爲工具欄設置條目
143             bar.SetItems(new UIBarButtonItem[]{ item},true);
144             //爲輸入框 鍵盤添加工具欄
145             fied.InputAccessoryView = bar;   
146 
147 //9、文本視圖菜單 退出按鈕
148          public override void ViewDidLoad()
149         { 
150             tf.Frame = new RectangleF(20,10,300,100); 
151             this.View.AddSubview(tf);
152             UIMenuItem item = new UIMenuItem("退出", new Selector("hide"));//添加菜單按鈕
153 
154             UIMenuController c = UIMenuController.SharedMenuController;
155             c.MenuItems = new UIMenuItem[] { item };
156 
157         }
158         UITextView tf = new UITextView();
159         [Export("hide")]
160         public void hide() {
161             //退出鍵盤
162             tf.ResignFirstResponder();
163         }  
164 
165 //10、觸摸背景隱藏鍵盤
166         public override void ViewDidLoad()
167         {
168             tf.Frame = new RectangleF(10,50,300,30);
169             tf.BorderStyle = UITextBorderStyle.RoundedRect;
170             this.View.AddSubview(tf);
171              
172         }
173         /// <summary>
174         ///重寫 ViewController 方法 TouchesBegan
175         /// </summary>
176         /// <param name="touches">Touches.</param>
177         /// <param name="evt">Evt.</param>
178         public override void TouchesBegan(NSSet touches, UIEvent evt)
179         {
180             tf.ResignFirstResponder();
181         }  
182// 11、進度條控件 UIProgressView 先在添加一個lable lbl 和一個button  sta
183          public override void ViewDidLoad()
184         {
185                 //按鈕的觸摸事件
186                 sta.TouchUpInside+=delegate {
187                 sta.Enabled = false;
188                 pv.Progress = 0f;
189                 //建立一個新的任務 用來計算 進度條的值 並更新視圖 
190                 Task.Factory.StartNew(this.Start);
191             };
192             /建立進度條 並添加到視圖 
193             pv = new UIProgressView(new RectangleF(60f,200f,200f,50f));
194             pv.Progress = 0f;
195             f = 1f / 10f;
196             this.View.AddSubview(pv);
197         }
198         UIProgressView pv;
199         float f = 0f;
200         public void Start() {
201             float p = 0f;
202             while (p<1f) {
203                 Thread.Sleep(1000);
204                 //調用主線程 修改視圖
205                 this.InvokeOnMainThread(delegate {
206                     pv.Progress += this.f;
207                     p = pv.Progress;
208                     lbl.Text = Math.Round(pv.Progress, 2).ToString();
209                     if (p >= 1f)
210                     {
211                         lbl.Text = "加載完成!";
212                         sta.Enabled = true;
213                     }
214                 });
215             }
216 
217         }  
218// 12、滾動視圖
219             (1).
220         UIImageView img;
221         UIScrollView scr;
222         public override void ViewDidLoad()
223         {
224             base.ViewDidLoad();
225             img = new UIImageView(UIImage.FromFile("1.png"));
226             scr = new UIScrollView();
227             scr.Frame = new RectangleF(0,0,320,568);
228             scr.ContentSize = img.Image.Size;            //滾動範圍
229             scr.ContentOffset = new PointF(200f,50f);   //目前滾動位置
230             scr.PagingEnabled = true;                   //能夠整頁翻動
231             scr.MinimumZoomScale=0.25f;                 //縮放最小比例
232             scr.MaximumZoomScale=2f;                    //放大的最大比例    
233             //獲取要縮放的圖像視圖
234             scr.ViewForZoomingInScrollView=delegate (UIScrollView scrll){
235                 return this.img;
236             };
237             scr.ZoomScale = 1f;                         //設置變化比例
238             scr.IndicatorStyle = UIScrollViewIndicatorStyle.Black; //滾動指示器的風格設置
239             scr.AddSubview(img);
240             this.View.AddSubview(scr);
241         }
242          (2).
243             UIScrollView scr = new UIScrollView();
244             scr.Frame = new RectangleF(0,0,325,568);
245             scr.ContentSize = new SizeF(320,2000);
246             this.View.AddSubview(scr);
247             //滾動視圖開始時調用 
248             scr.Scrolled+=delegate {
249                 Console.WriteLine("Start rolling");
250             };
251             //滾動視圖結束時調用 
252             scr.DecelerationEnded+=delegate {
253                 Console.WriteLine("Start End");
254             };
255             float y = 10;
256             for (float i = 1; i < 21; i++) {
257                 UILabel lab = new UILabel();
258                 lab.Frame = new RectangleF(0,y,320,50);
259                 lab.BackgroundColor = UIColor.Cyan;
260                 lab.Text = String.Format("{0}",i);
261                 scr.AddSubview(lab);
262                 y += 100;
263 
264             } 
265// 13、頁面控件
266         UIImageView img1;
267         UIImageView img2;
268         UIImageView img3;
269         UIScrollView scr;
270         UIPageControl pagc;
271         public override void ViewDidLoad()
272         {
273             base.ViewDidLoad();
274             //添加滾動視圖
275             scr = new UIScrollView();
276             scr.Frame = new RectangleF(0,0,320,495);
277             //滾動視圖結束滾動時調用
278             scr.DecelerationEnded += this.scr_DecelerationEnded;
279             //添加頁面
280             pagc = new UIPageControl();
281             pagc.Frame = new RectangleF(0,540,320,37);
282             pagc.Pages = 3; //設置頁面數值 及頁面小白點
283             //當頁面數值發生改變時調用
284             pagc.ValueChanged += this.pagc_ValueChanged;
285             //滾動視圖的滾動事件
286             scr.Scrolled+=delegate {
287                 Console.WriteLine("Scolled!");
288             };
289             scr.PagingEnabled = true;
290             RectangleF rf = (RectangleF)scr.Frame;
291             scr.ContentSize = new SizeF(rf.Width*3,rf.Height);
292             //添加圖像視圖
293             img1 = new UIImageView();
294             img1.ContentMode = UIViewContentMode.ScaleAspectFit;
295             img1.Image = UIImage.FromFile("1.jpg");
296             rf.X += (float)this.scr.Frame.Width;
297             img2 = new UIImageView();
298             img2.ContentMode = UIViewContentMode.ScaleAspectFit;
299             img2.Image = UIImage.FromFile("2.jpg");
300             rf.X += (float)this.scr.Frame.Width;
301             img3 = new UIImageView();
302             img3.ContentMode = UIViewContentMode.ScaleAspectFit;
303             img3.Image = UIImage.FromFile("3.jpg");
304             rf.X += (float)this.scr.Frame.Width;
305 
306             scr.AddSubview(img1);
307             scr.AddSubview(img2);
308             scr.AddSubview(img3);
309             this.View.AddSubview(scr);
310             this.View.AddSubview(pagc);
311 
312         }
313 
314         public void scr_DecelerationEnded(object sender,EventArgs e) {
315             float x1 = (float)this.img1.Frame.X;//獲取圖像img1 的x位置
316             float x2 = (float)this.img2.Frame.X;//獲取圖像img2的x位置
317             float x = (float)this.scr.ContentOffset.X;//獲取視圖對象scrolview scr 目前滾動的x位置
318 
319             //判斷x是否和x1相等
320             if (x == x1)
321             {
322                 this.pagc.CurrentPage = 0;
323 
324             }
325             else if (x == x2)
326             {
327                 this.pagc.CurrentPage = 1;
328 
329             }
330             else 
331             { 
332                 this.pagc.CurrentPage = 2;
333             
334             }
335 
336         }
337         public void pagc_ValueChanged(object sender, EventArgs e)
338         {
339             PointF pf = (PointF)this.scr.ContentOffset;
340             switch (this.pagc.CurrentPage) {
341                 case 0:
342                     pf.X = (float)this.img1.Frame.X;
343                     this.scr.SetContentOffset(pf,true);
344                     break;
345                 case 1:
346                     pf.X = (float)this.img2.Frame.X;
347                     this.scr.SetContentOffset(pf, true);
348                     break;
349                 case 2:
350                     pf.X = (float)this.img3.Frame.X;
351                     this.scr.SetContentOffset(pf, true);
352                     break;
353                 default:
354                     break;
355             }
356         }   
357// 14、警告視圖
358             UIAlertView a = new UIAlertView();
359             a.Title = "提示";
360             a.Message = "電量不足";
361             a.AddButton("肯定");
362             a.Show();  

 

一、按鈕  UIButton
                            UIButton   btn   =   new   UIButton () ;
            btn.Frame = new RectangleF(150,310,80,30); //按鈕位置一件寬高
            btn.SetTitle("Button",UIControlState.Normal); //顯示的文字
            btn.SetTitleColor(UIColor.Black,UIControlState.Normal);//文字的顏色
            btn.TouchUpInside += this.SetBackGroudColor; //按鈕 觸摸事件
            this.View.AddSubview(btn); //添加到當前視圖

二、圖像視圖 
UIImageView
            UIImageView img=new UIImageView();
            img.Frame = new RectangleF(0,0,375,667); //設置位置大小
            img.ContentMode = UIViewContentMode.ScaleAspectFit; //設置圖像視圖模式
            img.Image = UIImage.FromFile("img.jpg"); //設置圖像 

            img.Transform = CGAffineTransform.MakeRotation(20);//圖像旋轉正數順時針旋轉,負數逆時針旋轉
  
            img.Transform = CGAffineTransform.MakeScale(-2,2);  //圖像縮放
            
this.View.AddSubview(img); //添加到當前視圖
    

三、label控件 
UILabel  
             UILabel lbl1 = new UILabel();
            lbl1.Frame = new RectangleF(2,410,155,28);
            lbl1.Text = "UILabel";
            lbl1.Lines = 3; //顯示行數 配合     label高度 使用
            
lbl1.LineBreakMode = UILineBreakMode.MiddleTruncation; //內容顯示樣式 
            lbl1 .Hidden = true;  //是否顯示
            this.View.AddSubview(lbl1);
  

四、輸入框  UITextField
            UITextField   t1 = new UITextField();
            t1.BorderStyle = UITextBorderStyle.RoundedRect;
            t1.Frame = new RectangleF(50,250,300,30);
            t1.Placeholder = "帳號";
            //  限制輸入字符長度
            t1.ShouldChangeCharacters = (textField, range, replacementString) => {
                if (range.Location < 10)
                    return true;
                else
                    return false;
            };
            this.View.AddSubview(t1);
            
UITextField  t2   =   new   UITextField () ;  
            t2.BorderStyle = UITextBorderStyle.RoundedRect;//邊框樣式
            t2.Frame = new RectangleF(50, 300, 300, 30);
            t2.Placeholder = "密碼";
            t2.SecureTextEntry = true; //隱藏文本
            this.View.AddSubview(t2);
  


            UITextView text = new UITextView();
            text.Frame = new RectangleF(9, 90, 302, 180);

            this.View.AddSubview(text);
五、
UITextView 實現功能:點擊完成按鈕隱藏鍵盤,而且把text的Text值賦值給text1控件,並監控 text控件的狀態(開始輸入,結束輸入,輸入中)
            UITextView text1 = new UITextView();
            text1.Frame = new RectangleF(9, 330, 302, 180);
            text1.Editable = false;
            this.View.AddSubview(text1);
            text1.Hidden = true;
            UIButton btn = new UIButton();
            btn.Frame = new RectangleF(137,56,46,30);
            btn.SetTitle("完成", UIControlState.Normal);
            btn.SetTitleColor(UIColor.Red,UIControlState.Normal);
            this.View.AddSubview(btn);
            btn.TouchUpInside += (sender, e) => {
                text.ResignFirstResponder();
                text1.Hidden=false;
                text1.Text = text.Text;

            };
            text.Delegate = new MyDelegate();

        }
        private class MyDelegate : UITextViewDelegate
        {
            public override void EditingEnded(UITextView textView)
            {
                Console.WriteLine("Edit end");
            }
            public override void EditingStarted(UITextView textView)
            {
                Console.WriteLine("Edit start");
            }
            public override void Changed(UITextView textView)
            {
                Console.WriteLine("Edit ");
            }
        }
  
六、 輸入框鍵盤 樣式類型 設置
            UITextField fied = new UITextField();
            fied.Frame = new RectangleF(50, 100, 300, 30);
            fied.BorderStyle = UITextBorderStyle.Line; 
            this.View.AddSubview(fied);
  
            fied.KeyboardType = UIKeyboardType.Url;//設置鍵盤類型
            fied.KeyboardAppearance = UIKeyboardAppearance.Dark;// 設置鍵盤外觀
            fied.ReturnKeyType = UIReturnKeyType.Next;//設置鍵盤的Return鍵
  

七、顯示鍵盤 時改變輸入框的位置
                  UITextField   fied   =   new   UITextField () ;
        private NSObject kbdWillShow, kbdDidHide;

        public override void ViewDidLoad()
        {
            base.ViewDidLoad(); 
            fied.Frame = new RectangleF(10, 500, 300, 30);
            fied.BorderStyle = UITextBorderStyle.RoundedRect; 
             this.View.AddSubview(fied);
            //顯示鍵盤前 (事件)
            kbdWillShow = UIKeyboard.Notifications.ObserveWillShow((s, e) => {
                RectangleF kbdRounds = (RectangleF)e.FrameEnd;
                RectangleF textFrame = (RectangleF)fied.Frame;
                textFrame.Y -= kbdRounds.Height;
                fied.Frame = textFrame;
            });
            //隱藏鍵盤後 
 (事件)
            kbdDidHide = UIKeyboard.Notifications.ObserveDidHide((s, e) =>
            {
                RectangleF kbdRounds = (RectangleF)e.FrameEnd;
                RectangleF textFrame = (RectangleF)fied.Frame;
                textFrame.Y += kbdRounds.Height;
                fied.Frame = textFrame;
            });
            //鍵盤上的return鍵 隱藏鍵盤
 (事件)
            fied.ShouldReturn=delegate(UITextField textField) {
                return textField.ResignFirstResponder();
            };
        }
 八、 爲輸入框鍵盤 添加工具欄
            UITextField fied = new UITextField();
            fied.Frame = new RectangleF(10, 200, 300, 30);
            fied.BorderStyle = UITextBorderStyle.RoundedRect;//設置輸入框邊框
             this.View.AddSubview(fied);
            fied.KeyboardType = UIKeyboardType.EmailAddress;//設置鍵盤類型
            //設施工具欄
            UIToolbar bar = new UIToolbar();//實例化工具欄
            bar.SizeToFit();
            bar.BackgroundColor = UIColor.DarkGray;//設置背景
            //實例化 工具欄 按鈕
            UIBarButtonItem item = new UIBarButtonItem("完成", UIBarButtonItemStyle.Done, (s, e) =>
            {
                //隱藏鍵盤
                fied.ResignFirstResponder();
            });
            //爲工具欄設置條目
            bar.SetItems(new UIBarButtonItem[]{ item},true);
            //爲輸入框 鍵盤添加工具欄
            fied.InputAccessoryView = bar;
    

九、文本視圖菜單 退出按鈕
          public   override   void   ViewDidLoad ()
         {  
            tf.Frame = new RectangleF(20,10,300,100); 
            this.View.AddSubview(tf);
            UIMenuItem item = new UIMenuItem("退出", new Selector("hide"));//添加菜單按鈕

            UIMenuController c = UIMenuController.SharedMenuController;
            c.MenuItems = new UIMenuItem[] { item };

        }
        UITextView tf = new UITextView();
        [Export("hide")]
        public void hide() {
            //退出鍵盤
            tf.ResignFirstResponder();
        }
  

十、觸摸背景隱藏鍵盤
        public   override   void   ViewDidLoad ()
         {
            tf.Frame = new RectangleF(10,50,300,30);
            tf.BorderStyle = UITextBorderStyle.RoundedRect;
            this.View.AddSubview(tf);
             
        }
        /// <summary>
        ///重寫 ViewController 方法 TouchesBegan
        /// </summary>
        /// <param name="touches">Touches.</param>
        /// <param name="evt">Evt.</param>
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            tf.ResignFirstResponder();
        }
  
十一、進度條控件  UIProgressView 先在添加一個lable lbl 和一個button   sta
         public   override   void   ViewDidLoad ()
         {
                //按鈕的觸摸事件

                sta.TouchUpInside+=delegate {
                sta.Enabled = false;
                pv.Progress = 0f;
                //建立一個新的任務 用來計算 進度條的值 並更新視圖 

                Task.Factory.StartNew(this.Start);
            };
            /建立進度條 並添加到視圖 

            pv = new UIProgressView(new RectangleF(60f,200f,200f,50f));
            pv.Progress = 0f;
            f = 1f / 10f;
            this.View.AddSubview(pv);
        }
        UIProgressView pv;
        float f = 0f;
        public void Start() {
            float p = 0f;
            while (p<1f) {
                Thread.Sleep(1000);
                //調用主線程 修改視圖
                this.InvokeOnMainThread(delegate {
                    pv.Progress += this.f;
                    p = pv.Progress;
                    lbl.Text = Math.Round(pv.Progress, 2).ToString();
                    if (p >= 1f)
                    {
                        lbl.Text = "加載完成!";
                        sta.Enabled = true;
                    }
                });
            }

        }
  
十二、滾動視圖
            
(1).
        UIImageView img;
        UIScrollView scr;
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            img = new UIImageView(UIImage.FromFile("1.png"));
            scr = new UIScrollView();
            scr.Frame = new RectangleF(0,0,320,568);
            scr.ContentSize = img.Image.Size;            //滾動範圍
            scr.ContentOffset = new PointF(200f,50f);   //目前滾動位置
            scr.PagingEnabled = true;                   //能夠整頁翻動
            scr.MinimumZoomScale=0.25f;                 //縮放最小比例
            scr.MaximumZoomScale=2f;                    //放大的最大比例    
            //獲取要縮放的圖像視圖
            scr.ViewForZoomingInScrollView=delegate (UIScrollView scrll){
                return this.img;
            };
            scr.ZoomScale = 1f;                         //設置變化比例
            scr.IndicatorStyle = UIScrollViewIndicatorStyle.Black; //滾動指示器的風格設置
            scr.AddSubview(img);
            this.View.AddSubview(scr);
        }
         (2).
            
UIScrollView   scr   =   new   UIScrollView () ;

            scr.Frame = new RectangleF(0,0,325,568);
            scr.ContentSize = new SizeF(320,2000);
            this.View.AddSubview(scr);

            //滾動視圖開始時調用 

            scr.Scrolled+=delegate {
                Console.WriteLine("Start rolling");
            };
            //滾動視圖結束時調用 

            scr.DecelerationEnded+=delegate {
                Console.WriteLine("Start End");
            };
            float y = 10;
            for (float i = 1; i < 21; i++) {
                UILabel lab = new UILabel();
                lab.Frame = new RectangleF(0,y,320,50);
                lab.BackgroundColor = UIColor.Cyan;
                lab.Text = String.Format("{0}",i);
                scr.AddSubview(lab);
                y += 100;

ide

             }  
1三、頁面控件
        UIImageView img1;
        UIImageView img2;
        UIImageView img3;
        UIScrollView scr;
        UIPageControl pagc;
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            //添加滾動視圖
            scr = new UIScrollView();
            scr.Frame = new RectangleF(0,0,320,495);
            //滾動視圖結束滾動時調用
            scr.DecelerationEnded += this.scr_DecelerationEnded;
            //添加頁面
            pagc = new UIPageControl();
            pagc.Frame = new RectangleF(0,540,320,37);
            pagc.Pages = 3; //設置頁面數值 及頁面小白點
            //當頁面數值發生改變時調用
            pagc.ValueChanged += this.pagc_ValueChanged;
            //滾動視圖的滾動事件
            scr.Scrolled+=delegate {
                Console.WriteLine("Scolled!");
            };
            scr.PagingEnabled = true;
            RectangleF rf = (RectangleF)scr.Frame;
            scr.ContentSize = new SizeF(rf.Width*3,rf.Height);
            //添加圖像視圖
            img1 = new UIImageView();
            img1.ContentMode = UIViewContentMode.ScaleAspectFit;
            img1.Image = UIImage.FromFile("1.jpg");
            rf.X += (float)this.scr.Frame.Width;
            img2 = new UIImageView();
            img2.ContentMode = UIViewContentMode.ScaleAspectFit;
            img2.Image = UIImage.FromFile("2.jpg");
            rf.X += (float)this.scr.Frame.Width;
            img3 = new UIImageView();
            img3.ContentMode = UIViewContentMode.ScaleAspectFit;
            img3.Image = UIImage.FromFile("3.jpg");
            rf.X += (float)this.scr.Frame.Width;

            scr.AddSubview(img1);
            scr.AddSubview(img2);
            scr.AddSubview(img3);
            this.View.AddSubview(scr);
            this.View.AddSubview(pagc);

        }

        public void scr_DecelerationEnded(object sender,EventArgs e) {
            float x1 = (float)this.img1.Frame.X;//獲取圖像img1 的x位置
            float x2 = (float)this.img2.Frame.X;//獲取圖像img2的x位置
            float x = (float)this.scr.ContentOffset.X;//獲取視圖對象scrolview scr 目前滾動的x位置

            //判斷x是否和x1相等
            if (x == x1)
            {
                this.pagc.CurrentPage = 0;

            }
            else if (x == x2)
            {
                this.pagc.CurrentPage = 1;

            }
            else 
            { 
                this.pagc.CurrentPage = 2;
            
            }

        }
        public void pagc_ValueChanged(object sender, EventArgs e)
        {
            PointF pf = (PointF)this.scr.ContentOffset;
            switch (this.pagc.CurrentPage) {
                case 0:
                    pf.X = (float)this.img1.Frame.X;
                    this.scr.SetContentOffset(pf,true);
                    break;
                case 1:
                    pf.X = (float)this.img2.Frame.X;
                    this.scr.SetContentOffset(pf, true);
                    break;
                case 2:
                    pf.X = (float)this.img3.Frame.X;
                    this.scr.SetContentOffset(pf, true);
                    break;
                default:
                    break;
            }
        }
    

1四、警告視圖
            UIAlertView a = new UIAlertView();
            a.Title = "提示";
            a.Message = "電量不足";
            a.AddButton("肯定");
            a.Show();
  工具

相關文章
相關標籤/搜索