Implement banner loop carousel and iosbanner loop in IOS

Source: Internet
Author: User

Implement banner loop carousel and iosbanner loop in IOS

In the project, place the banner image UIImageView one by one into UIScrollView. By setting the pagingEnabled attribute of UIScrollView to YES, you can display the image one by one when the user slides the banner, however, when a user wants to move from the first image to the last image, or from the last image to the first image, the user will be stuck. The simple method is to determine the direction of the user's swipe, then, let UIScrollView automatically slide to the last or first image. This is not natural in the animation. Here is the solution.

Assume that three banner images are arranged in the banner's UIScrollView in the following way:

Third First Second Third First

UIScrollView scrolls to the second image by default. When the user slides to the first image, UIScrollView automatically scrolls to 4th. Similarly, when the user slides to the last image, UIScrollView automatically scrolls to the second image, and the animation effect is set to NO.

The Code is as follows:

H file

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UIScrollView *mainScroll;@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;@end

M file

# Import "ViewController. h "@ interface ViewController () <response> {CGFloat w; NSMutableArray * imgArr; NSMutableArray * pageArr;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; w = self. view. frame. size. width; // page count pageArr = [NSMutableArray array]; for (int I = 1; I <= 3; I ++) {[pageArr addObject: [NSString stringWithFormat: @ "mongod.jpg", I];} // obtain the number of imgviews again and put the last displayed image in the first one, while imgArr = [NSMutableArray array]; [imgArr addObject: pageArr [pageArr. count-1]; for (NSString * imgSrc in pageArr) {[imgArr addObject: imgSrc];} [imgArr addObject: pageArr [0]; self. mainScroll. contentSize = CGSizeMake (w * imgArr. count, 0); // start to insert the image for (int I = 0; I 

 

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.