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