Uiscrollview Control for iOS development (18)

Source: Internet
Author: User
1 Preface

When some content needs to be displayed on the screen, but the screen size cannot provide the required area, we need to consider using the uiscrollview control.

2. code example

Zyviewcontroller. h:

#import <UIKit/UIKit.h>@interface ZYViewController : UIViewController<UIScrollViewDelegate>@property(nonatomic,strong) UIImageView *myImageView;@property(nonatomic,strong) UIScrollView *myScrollView;@end

Zyviewcontroller. M:

@ Synthesize myimageview; @ synthesize myscrollview;-(void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view, typically from a nib. self. view. backgroundcolor = [uicolor whitecolor]; uiimage * imagetoload = [uiimage imagenamed: @ "apple.png"]; self. myimageview = [[uiimageview alloc] initwithimage: imagetoload]; self. myscrollview = [[uiscrollview alloc] initwithframe: Self. view. bounds]; // initialize the uiscrollerview control [self. myscrollview addsubview: Self. myimageview]; // Add the image to uiscrollerview self. myscrollview. contentsize = self. myimageview. bounds. size; // set the image size to uiscrollerview self. myscrollview. indicatorstyle = uiscrollviewindicatorstylewhite; // The sliding view is set to white. delegate = self; // set the proxy of the scroll view to self [self. view addsubview: Self. myscrollview]; // Add uiscrollerview} # pragma mark uiscrollview // call this method when the content slides-(void) scrollviewdidscroll :( uiscrollview *) scrollview {self. myscrollview. alpha = 0.5f; nslog (@ "Call the scrollviewdidscroll method");} // when sliding the view finger away from the screen and the content is still sliding-(void) scrollviewdidenddecelerating :( uiscrollview *) scrollview {self. myscrollview. alpha = 1.0f; nslog (@ "Call the scrollviewdidenddecelerating method");} // call the-(void) scrollviewdidenddragging (uiscrollview *) scrollview willdecelerate :( bool) when the drag is completed) decelerate {self. myscrollview. alpha = 1.0f; nslog (@ "Call scrollviewdidenddragging method ");}

Running result:

Before sliding:


Slide:


Console display content when dragging:

21:40:40. 586 uiscrollviewtest [676: c07]CallScrollviewdidscrollMethod

21:40:42. 986 uiscrollviewtest [676: c07]CallScrollviewdidenddraggingMethod

Console display content when sliding:

21:43:37. 789 uiscrollviewtest [676: c07]CallScrollviewdidscrollMethod

21:43:37. 789 uiscrollviewtest [676: c07]CallScrollviewdidenddeceleratingMethod

Paging code:

Zyuiscrollviewcontroller. h:

#import <UIKit/UIKit.h>@interface ZYUIScrollViewController : UIViewController@property(nonatomic,strong) UIScrollView *myScrollView;@end

Zyuiscrollviewcontroller. M:

-(Void) viewdidload {[Super viewdidload]; // do any additional setup after loading the view. self. view. backgroundcolor = [uicolor whitecolor]; // instantiate three images: uiimage * iPhone = [uiimage imagenamed: @ "iphone.png"]; uiimage * iPod = [uiimage imagenamed: @ "ipod.png"]; uiimage * macbookair = [uiimage imagenamed: @ "mac.png"]; cgrect scrollviewrect = self. view. bounds; // initialize a screen View Size rectangle border self. myscrollview = [[uiscrollview alloc] initwithframe: scrollviewrect]; // initialize the uiscrollview Control Self. myscrollview. pagingenabled = yes; // start pagination self. myscrollview. contentsize = cgsizemake (scrollviewrect. size. width * 3.0f, scrollviewrect. size. height); // set the uiscrollview size [self. view addsubview: Self. myscrollview]; // Add uiscrollview cgrect imageviewrect = self. view. bounds; uiimageview * iphoneimageview = [[uiimageview alloc] initwithimage: iPhone]; // initialize the Image view [iphoneimageview setframe: imageviewrect]; // set the uiimage size [self. myscrollview addsubview: iphoneimageview]; imageviewrect. origin. X + = imageviewrect. size. width; // the abscissa of imageviewrect moves to the right the distance from the original imageviewrect width to uiimageview * ipodimageview = [[uiimageview alloc] initwithimage: IPOD]; [ipodimageview setframe: imageviewrect. myscrollview addsubview: ipodimageview]; imageviewrect. origin. X + = imageviewrect. size. width; uiimageview * macbookairimageview = [[uiimageview alloc] initwithimage: macbookair]; [macbookairimageview setframe: imageviewrect]; [self. myscrollview addsubview: macbookairimageview];}

Running result:




3 conclusion

The preceding figure shows the simple usage of the uiscrollview control, which is helpful to you.

Related Article

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.