IOS base Control (bottom)

Source: Internet
Author: User

The previous article introduced UIButton, UILabel, Uiimageview and Uitextfield, which briefly introduced Uiscrollview and Uialertview.

Uiscrollview

As the name implies also know that this is a scrolling related control, in the development of Android encountered ScrollView, when the size of the content beyond the screen, with the ScrollView can be scrolled to make the part of the screen beyond the display by scrolling way, In Android there are horizontal ScrollView and vertical scrollview, there is only one scrollview in iOS, and this scrollview is more powerful, some functions are beyond the scope of ScrollView. The following is a look at some of the properties of ScrollView

    • Contentsize:cgsize type, the actual size of the content of the ScrollView;
    • The Contentoffset:cgpoint type, scrollview the position currently scrolled to, is positioned in the upper left corner of the view;
    • The contentinset:uiedgeinsets type, used to increase the scrolling range of the ScrollView content, is equivalent to the surrounding filler of the ScrollView;

The following are other properties

    • Bounces:bool type, whether it has a spring effect
    • Scrollenabled:bool type, whether it can scroll
    • Showshorizontalscrollindicator:bool type, whether the horizontal scroll bar is displayed
    • Showsverticalscrollindicator:bool type, whether the vertical scroll bar is displayed
    • Indicatorstyle:uiscrollviewindicatorstyle type, sets the style of the scroll bar, which has three values for the enumeration type
   Uiscrollviewindicatorstyledefault   uiscrollviewindicatorstyleblack   uiscrollviewindicatorstylewhite
    • Dragging:bool type, whether it is being dragged
    • Tracking:bool type, the value is yes when touch is not yet dragged, otherwise no
    • Decelerating:bool type, whether it is slowing down
    • Zooming:bool type, whether you are scaling

The most straightforward feature of ScrollView is sliding, but it also has gesture scaling (which requires the matrix in ImageView in Android), as well as sliding page flipping (which is achieved via Viewpager in Android). The following code is implemented by adding the Viewdidload method to the

    UIImage *image=[uiimage imagenamed:@ "Android struct2.gif"];    Self.imageview=[[Uiimageview alloc]initwithimage:image];    Self.imageView.tag=;    [Self.scrollview AddSubview:self.imageView];

Simply through the above code can be realized sliding to see a picture of the function, a uiscrollview is actually a container, it is inside the sub-space is added through the Addsubview method, The most important thing is to tell Uiscrollview exactly how large the child control is by setting the Contentsize property.

The example of gesture scaling is modified on the basis of the above, if only to realize the gesture scaling, mainly realizes the Uiscrollview-(UIView *) Viewforzoominginscrollview: (Uiscrollview *) ScrollView method, also set the maximum minimum scale of Uiscrollview: Minimumzoomscale attribute and Maximumzoomscale attribute, add in Viewdidload method

    UIImage *image=[uiimage imagenamed:@ "Android struct2.gif"];    Self.imageview=[[Uiimageview alloc]initwithimage:image];    Self.imageView.tag=;    [Self.scrollview AddSubview:self.imageView];        Self.scrollView.contentSize=imageView.image.size;    Self.scrollView.maximumZoomScale=5;    Self.scrollView.minimumZoomScale=0.2;    Self.scrollview. delegate=self;

Add the following method to the class in the View controller

-(uiview*) Viewforzoominginscrollview: (uiscrollview*) scrollview{    return  Self.imageview ;}

If you want to add some extra gestures you need to use UITapGestureRecognizer to add the Viewdidload

    UITapGestureRecognizer *twofingertaprecongizer=[[UITapGestureRecognizer alloc] initwithtarget:self action:@ Selector (scrollviewtwofingertapped)];    twofingertaprecongizer.numberoftapsrequired=1;    twofingertaprecongizer.numberoftouchesrequired=2;    [Self.scrollview Addgesturerecognizer:twofingertaprecongizer];

Then define the following method

-(void) scrollviewtwofingertapped: (uitapgesturerecognizer*) recognizer{    cgfloat Newzoomscale =self.scrollview.zoomscale/1.5f;     =MAX (newzoomscale,self.scrollview.minimumzoomscale);        [Self.scrollview Setzoomscale:newzoomscale Animated: true ];}

Next is the paging function, to want to turn the page function is best to combine another control Uipagecontrol, about the control has a few properties to enumerate

    • Numberofpages: Total Pages
    • CurrentPage: The current page number
    • Hidesforsinglepage: When there is only one page, do you want to hide the view listener

To get Uiscrollview into paging mode, you need to set the Pagingenabled property to set it to true. The code below, added in Viewdidload

1CGFloat w=Self.view.frame.size.width;2CGFloat h=Self.view.frame.size.height;3      for(intI=0;i<3; i++)4     {5Uiimageview *imageview=[[Uiimageview alloc]init];6Imageview.frame=cgrectmake (I*w,0, W, h);7Imageview.image=[uiimage imagenamed:@"African daisy.gif"];8         9 [Self.scrollview Addsubview:imageview];Ten     } One      ASelf.scrollview.contentsize=cgsizemake (3*w,0); -Self.scrollview.showshorizontalscrollindicator=false; -Self.scrollview.pagingenabled=true; theSelf.scrollview.Delegate=Self ; -  -Uipagecontrol *pagecontrol=[[Uipagecontrol alloc] init]; -Pagecontrol.center=cgpointmake (w*0.5, H- -); +Pagecontrol.bounds=cgrectmake (0,0, Max, -); -pagecontrol.numberofpages=3; +  APagecontrol.pageindicatortintcolor=[Uicolor Graycolor]; atPagecontrol.currentpageindicatortintcolor=[Uicolor Whitecolor]; -  -Pagecontrol.enabled=false; - [Self.view Addsubview:pagecontrol]; -_pagecontrol=Pagecontrol; -  in-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView - { to     intpage=scrollview.contentoffset.x/ScrollView.frame.size.width; +_pagecontrol.currentpage=page; -}
Uialertview

Uialertview is the message pop-up window, its effect and effect do not need to say, to use uialertview needs to let the controller implement Uialertviewdelegate protocol. The properties that are useful are as follows

    • Title: The caption of the message box
    • Message: The contents of the Messages box
    • NumberOfButtons: Total number of buttons
    • Cancelbuttontitle: The title of the Cancel button
    • Cancelbuttonindex: The index of the Cancel button
    • Firstotherbuttonindex: Index of the first other type of button
    • Visible: Message box visible
    • The Alertviewstyle:uialertviewstyle type, which is the type of the message box, is an enumeration type, and its value is as follows
    • Uialertviewstyledefault Only Information and buttons
    • Uialertviewstylesecuretextinput has a TextField encryption box
    • Uialertviewstyleplaintextinput has a non-encrypted TextField
    • Uialertviewstyleloginandpasswordinput has two textfield,login and password

For the button index mentioned above, is a set of Cancel button and other buttons, where the first is usually the Cancel button, its index is 0, if the Cancel button is not set, then it will be-1, others in the order of addition. In the iOS message box, only the cancel has a unique name, and the other buttons do not have a unique name, collectively known as Otherbutton.

One of the constructor functions of a message box is as follows

Uialertview *alert = [[Uialertview alloc] Initwithtitle:@"alertviewtest"message:@"message"                                                    Delegate: Self cancelbuttontitle:@"Cancel"Otherbuttontitles:@"OTHERBTN1",@"OtherBtn2",@"OtherBtn3", nil];

The above Otherbuttontitles parameter allows you to add multiple buttons. Add a button In addition to the constructor inside, you can also add through the method. If the Cancel button is not required, pass a nil value to the parameter

[Alert addbuttonwithtitle:@ "addbutton"];

To have the message box appear, you need to call the following method

[Alert show];

The message box also has a series of events

-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) buttonindex{     // This event is triggered after the button on the message box is clicked, and the Buttonindex determines which button is clicked and then takes the appropriate action }

There are 6 other event methods

1-(void) Alertview: (Uialertview *) Alertview Diddismisswithbuttonindex: (nsinteger) Buttonindex2 {3     //events executed when the Alertview has disappeared4 }5 6-(void) Alertview: (Uialertview *) Alertview Willdismisswithbuttonindex: (nsinteger) Buttonindex7 {8     //events When Alertview is about to disappear9 }Ten  One-(void) Alertviewcancel: (Uialertview *) Alertview A { -     //Alertview The event of the Cancel button, but this event has not been triggered by me - } the  --(void) Didpresentalertview: (Uialertview *) Alertview - { -     //Alertview events that have been shown + } -  +-(void) Willpresentalertview: (Uialertview *) Alertview A { at     //Alertview is about to be displayed - } -  --(BOOL) Alertviewshouldenablefirstotherbutton: (Uialertview *) Alertview - { -     //the first triggered event in}

For the above series of events can be broadly divided into two categories, showing the events, their triggering sequence is as follows

Alertviewshouldenablefirstotherbutton-->willpresentalertview-->didpresentalertview

The other is the Vanishing event, which is triggered only after a click.

Clickedbuttonatindex--> (Call Alertviewcancel If a view cancellation is triggered) willdismisswithbuttonindex--> Diddismisswithbuttonindex

IOS base Control (bottom)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.