Xamarin iOS Tutorial page Controls Xamarin iOS page controls
In the iphone's main interface, you will often see a row of small white spots, that is, the page control, 2.44 is shown. It is made up of small white dots and scrolling views that can be used to control page flipping. When scrolling through a view, you can see the position of the current page through a small white dot in the page control, or you can scroll to the specified page by tapping the small white point in the page control.
Figure 2.44 Page controls
In this illustration, the current page corresponding to the small white point is highlighted. This control indicates that the content is divided into two pages.
"Example 2-26" The following will use Page view to control the display of the image. The steps are as follows:
(1 Create a single View application type of project named 2-11.
(2 Add images 1.jpg, 2.jpg, 3.jpg to the resources folder where the project was created.
(3 Open the 2-11viewcontroller.cs file and write code that implements the display of the control image using the scrolling view. The code is as follows:
- Using System;
- Using System.Drawing;
- Using Monotouch.foundation;
- Using Monotouch.uikit;
- Namespace Application
- {
- public partial class __11viewcontroller:uiviewcontroller
- {
- Uiimageview Page1;
- Uiimageview Page2;
- Uiimageview Page3;
- Uiscrollview ScrollView;
- Uipagecontrol Pagecontrol;
- ...//This omits the construction method and the destructor method of the view controller.
- #region View Lifecycle
- public override void Viewdidload ()
- {
- Base. Viewdidload ();
- Perform any additional setup after loading the view, typically from a nib.
- Add a scrolling View object ScrollView
- ScrollView = new Uiscrollview ();
- Scrollview.frame = new RectangleF (0, 0, 320, 495);
- Method called when scrolling the view ends scrolling
- scrollview.decelerationended + = this.scrollview_decelerationended;
- Add a page
- Pagecontrol = new Uipagecontrol ();
- Pagecontrol.frame = new RectangleF (0, 540, 320, 37);
- pagecontrol.pages = 3; //sets the number of pages of the page control, which is the small white dot
- Called when the value of the page control has changed
- Pagecontrol.valuechanged + = this.pagecontrol_valuechanged;
- Scrolling events for scroll views
- scrollview.scrolled + = delegate {
- Console.WriteLine ("scrolled!");
- } ;
- Scrollview.pagingenabled = true;
- RectangleF pageframe = scrollview.frame;
- Scrollview.contentsize = new SizeF (Pageframe.width * 3, pageframe.height);
- Add an Image View Object Page1
- Page1 = new Uiimageview (pageframe);
- Page1. Contentmode = Uiviewcontentmode.scaleaspectfit;
- Page1. Image = Uiimage.fromfile ("1.jpg");
- Pageframe.x + = This.scrollView.Frame.Width;
- Add an Image View Object Page2
- Page2 = new Uiimageview (pageframe);
- Page2. Contentmode = Uiviewcontentmode.scaleaspectfit;
- Page2. Image = Uiimage.fromfile ("2.jpg");
- Pageframe.x + = This.scrollView.Frame.Width;
- Add an Image View Object Page3
- Page3 = new Uiimageview (pageframe);
- Page3. Contentmode = Uiviewcontentmode.scaleaspectfit;
- Page3. Image = Uiimage.fromfile ("3.jpg");
- Scrollview.addsubview (Page1);
- Scrollview.addsubview (Page2);
- Scrollview.addsubview (Page3);
- This. View.addsubview (ScrollView);
- This. View.addsubview (Pagecontrol);
- }
- private void Scrollview_decelerationended (object sender, EventArgs e)
- {
- float x1 = this.page1.frame.x; Gets the x position of the image View Object Page1
- float x2 = this.page2.frame.x; Gets the x position of the image View Object Page2
- float x = this.scrollview.contentoffset.x; Gets the scroll view object ScrollView the current scroll x position
- Determine if x is equal to X1
- if (x = = x1)
- {
- this.pageControl.CurrentPage = 0; to set the page control's current page
- } else if (x = = x2)//Determine if x is equal to X2
- {
- This.pageControl.CurrentPage = 1;
- } else
- {
- This.pageControl.CurrentPage = 2;
- }
- }
- private void Pagecontrol_valuechanged (object sender, EventArgs e)
- {
- PointF contentoffset = this.scrollView.ContentOffset;
- Use the switch statement to determine the current number of pages
- Switch (this.pageControl.CurrentPage)
- {
- Case 0:
- contentoffset.x = this.page1.frame.x;
- This.scrollView.SetContentOffset (Contentoffset, true); Sets the position where scrolling view is currently scrolled
- Break
- Case 1:
- Contentoffset.x = this.page2.frame.x;
- This.scrollView.SetContentOffset (Contentoffset, true);
- Break
- Case 2:
- Contentoffset.x = this.page3.frame.x;
- This.scrollView.SetContentOffset (Contentoffset, true);
- Break
- Default
- Break
- }
- }
- ...//This omits some methods before and after view loading and unloading
- #endregion
- }
- }
Run effect 2.45 as shown.
Figure 2.45 Running effect
In the page control, developers need to be aware of the following 2 questions:
1. Property settings for page controls
There are not many page control property settings, usually the number of pages to set and the current page. To set the number of pages for a page control, you need to use the Pages property in the following syntax:
- The page control object. pages= pages;
Where the number of pages is an integral type of data. To set the current page of a page control, you need to use the CurrentPage property, which has the following syntax:
- The page control object. currentpage= current page;
Where the current page is an integral type of data.
2. Response of the page control
The response of the page control needs to be implemented using the ValueChanged event. The code in example 2-26 is as follows:
- Pagecontrol.valuechanged + = this.pagecontrol_valuechanged;
This article is selected from: Xamarin iOS Development Combat University PA Internal information, reproduced please indicate the source, respect the technology respect the IT person!
The page controls for the Xamarin iOS tutorial