Basic iOS control (lower) and basic ios Control

Source: Internet
Author: User

Basic iOS control (lower) and basic ios Control

The previous section describes UIButton, UILabel, UIImageView, and UITextField. This section briefly introduces UIScrollView and UIAlertView.

UIScrollView

As the name implies, we also know that this is a scroll-related control. We encountered ScrollView during Android development. When the content size exceeds the screen size, scrollView can be used to display the content beyond the screen in scroll mode. Horizontal ScrollView and vertical ScrollView are available in Android, there is only one ScrollView in iOS, and this ScrollView has more functions, some of which are beyond the scope of ScrollView. The following describes some attributes of ScrollView.

  • ContentSize: Specifies the actual content size of ScrollView of the CGSize type;
  • ContentOffset: CGPoint type. the position to which ScrollView is currently rolled is located in the upper left corner of the view;
  • ContentInset: The UIEdgeInsets type. It is used to increase the scrolling range of the ScrollView content, which is equivalent to adding the surrounding whitelist of the ScrollView;

The following are other attributes.

  • Bounces: BOOL type, whether spring effect exists
  • ScrollEnabled: BOOL type, whether to scroll
  • ShowsHorizontalScrollIndicator: BOOL type, whether to display a horizontal scroll bar
  • ShowsVerticalScrollIndicator: BOOL type, whether to display a scroll bar in the vertical direction
  • IndicatorStyle: UIScrollViewIndicatorStyle type. Set the scroll bar style. This enumeration type has three values.
   UIScrollViewIndicatorStyleDefault   UIScrollViewIndicatorStyleBlack   UIScrollViewIndicatorStyleWhite
  • Dragging: BOOL type, whether it is being dragged
  • Tracking: BOOL type. The value is YES when the touch is not dragged. Otherwise, NO
  • Decelerating: BOOL type. Is it slowing down?
  • Zooming: BOOL type, scaling or not

The most direct function of ScrollView is to slide, but it also has both gesture Scaling (the Matrix needs to be used in ImageView in Android ), in addition, the page flip function (which is implemented by ViewPager in Android) is implemented through code below, and added to the viewDidLoad method.

    UIImage *image=[UIImage imageNamed:@"Android Struct2.gif"];    self.imageView=[[UIImageView alloc]initWithImage:image];    self.imageView.tag=22;    [self.scrollView addSubview:self.imageView];

By using the above code, you can slide to view an image. A UIScrollView is actually a container, and the sub-spaces in it are added through the addSubview method, the most important thing is to set the contentSize attribute to tell UIScrollView the size of the Child control.

The example of gesture scaling is modified based on the preceding figure. If you only need to implement gesture scaling, The UIScrollView-(UIView *) viewForZoomingInScrollView (UIScrollView *) scrollView method is implemented, at the same time, you must set the maximum and minimum scaling ratio of UIScrollView: minimumZoomScale attribute and maximumZoomScale attribute, and add them to the viewDidLoad method.

    UIImage *image=[UIImage imageNamed:@"Android Struct2.gif"];    self.imageView=[[UIImageView alloc]initWithImage:image];    self.imageView.tag=22;    [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;}

Use UITapGestureRecognizer to add some additional gestures to viewDidLoad.

    UITapGestureRecognizer *twoFingerTapRecongizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped)];    twoFingerTapRecongizer.numberOfTapsRequired=1;    twoFingerTapRecongizer.numberOfTouchesRequired=2;    [self.scrollView addGestureRecognizer:twoFingerTapRecongizer];

Define the following method.

-(void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer{    CGFloat newZoomScale=self.scrollView.zoomScale/1.5f;    newZoomScale =MAX(newZoomScale,self.scrollView.minimumZoomScale);        [self.scrollView setZoomScale:newZoomScale animated:true];}

Next we will talk about the page flip function. We 'd better combine the page flip function with another control UIPageControl. There are several attributes to list for this control.

  • NumberOfPages: Total number of pages
  • CurrentPage: Current page number
  • HidesForSinglePage: whether to hide view listening when there is only one page

To enable UIScrollView To Go To The paging mode, you must set the pagingEnabled attribute to True. The Code is as follows: add it to viewDidLoad

 1     CGFloat w=self.view.frame.size.width; 2     CGFloat h=self.view.frame.size.height; 3     for(int i=0;i<3;i++) 4     { 5         UIImageView *imageView=[[UIImageView alloc]init]; 6         imageView.frame=CGRectMake(i*w, 0, w, h); 7         imageView.image=[UIImage imageNamed:@"African Daisy.gif"]; 8          9         [self.scrollView addSubview:imageView];10     }11     12     self.scrollView.contentSize=CGSizeMake( 3*w, 0);13     self.scrollView.showsHorizontalScrollIndicator=false;14     self.scrollView.pagingEnabled=true;15     self.scrollView.delegate=self;16 17 UIPageControl *pagecontrol=[[UIPageControl alloc] init];18 pagecontrol.center=CGPointMake(w*0.5,h-20);19 pagecontrol.bounds=CGRectMake(0, 0, 150, 50);20 pagecontrol.numberOfPages=3;21 22 pagecontrol.pageIndicatorTintColor=[UIColor grayColor];23 pagecontrol.currentPageIndicatorTintColor=[UIColor whiteColor];24 25 pagecontrol.enabled=false;26 [self.view addSubview:pagecontrol];27 _pageControl=pagecontrol;28 29 -(void) scrollViewDidScroll:(UIScrollView *)scrollView30 {31     int page=scrollView.contentOffset.x/scrollView.frame.size.width;32     _pageControl.currentPage=page;33 }
UIAlertView

UIAlertView is a pop-up message window. You don't need to talk about its functions and effects. To use UIAlertView, You need to enable the Controller to implement the UIAlertViewDelegate protocol. The useful attributes are as follows:

  • Title: The title of the message box.
  • Message: the content of the message 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 button of another type
  • Visible: message frame visibility
  • AlertViewStyle: type of UIAlertViewStyle. It is a message frame type and an enumeration type. Its value is as follows:
  • UIAlertViewStyleDefault only displays information and buttons
  • UIAlertViewStyleSecureTextInput has a textfield encryption box
  • UIAlertViewStylePlainTextInput has an unencrypted textfield
  • UIAlertViewStyleLoginAndPasswordInput has two textfields, Login and password.

The button index mentioned above is a collection composed of the Cancel button and other buttons. The first one is generally the Cancel button, and its index is 0. If the Cancel button is not set, then it will be-1, and others will be in the order of addition. In the message box of iOS, only the Cancel has a special name, and other buttons do not have a special name, all of which are called OtherButton.

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

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AlertViewTest"                                                     message:@"message"                                                    delegate:self                                           cancelButtonTitle:@"Cancel"  otherButtonTitles:@"OtherBtn1",@"OtherBtn2",@"OtherBtn3",nil];

You can add multiple buttons to the above otherButtonTitles parameter. In addition to the constructor, the Add button can also be added through methods. If the Cancel button is not required, a nil value is passed to the parameter.

[alert addButtonWithTitle:@"addButton"];

To display the message box, call the following method:

[alert show];

There are also a series of events in the message box.

-(Void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex {// This event is triggered after the button on the message box is clicked, use buttonIndex to determine which button is clicked and then take the corresponding action}

There are 6 other event Methods

1-(void) alertView :( UIAlertView *) alertView didDismissWithButtonIndex :( NSInteger) buttonIndex 2 {3 // event executed when AlertView disappears 4} 5 6-(void) alertView :( UIAlertView *) alertView willDismissWithButtonIndex :( NSInteger) buttonIndex 7 {8 // events about to disappear when ALertView 9} 10 11-(void) alertViewCancel :( UIAlertView *) alertView12 {13 // The cancel button event of AlertView, but this event has never been triggered by 14} 15 16-(void) didPresentAlertView :( UIAlertView *) alertView17 {18 // event 19 when AlertView is displayed} 20 21-(void) willPresentAlertView :( UIAlertView *) alertView22 {23 // when AlertView is about to be displayed 24} 25 26-(BOOL) alertViewShouldEnableFirstOtherButton :( UIAlertView *) alertView27 {28 // The first event 29}

The preceding series of events can be divided into two categories. The sequence of events displayed is as follows:

AlertViewShouldEnableFirstOtherButton --> willPresentAlertView --> didPresentAlertView

The other is the event that disappears. It is triggered only after a click.

ClickedButtonAtIndex --> (if view cancellation is triggered, alertViewCancel is called) willDismissWithButtonIndex --> didDismissWithButtonIndex

 

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.