Scroll through the scrollView loop --- the last image is followed by the first image. The first image is followed by the last image.
Problem description: scrollView-the last image is followed by the first image. The first image is placed in front of the last image to form a ring and switch the image cyclically. As follows:
The Code is as follows:
// ViewController. m
# Import "ViewController. h"
# Define kW 375
# Define kH 500
# Define kCount 11
@ Interface ViewController () <UIScrollViewDelegate>
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[SuperviewDidLoad];
// Load the scroll View
[Self_loadScroll];
}
-(Void) _ loadScroll
{
UIScrollView * scrollView = [[UIScrollViewalloc] initWithFrame: CGRectMake (0, kW, kH)];
ScrollView. delegate = self;
For (int I = 0; I <kCount + 2; I ++)
{
UIImageView * imageView = [[UIImageViewalloc] initWithFrame: CGRectMake (I * kW, 0, kW, kH)];
// Image serial number
Int imageIndex = I;
// Special processing is required for the first and last images.
If (I = 0)
{
ImageIndex = kCount;
}
Else if (I = kCount + 1)
{
ImageIndex = 1;
}
// Add an image
NSString * imgName = [NSStringstringWithFormat: @ "tpolici.jpg", imageIndex];
ImageView. image = [UIImageimageNamed: imgName];
[ScrollViewaddSubview: imageView];
}
ScrollView. contentSize = CGSizeMake (kCount + 2) * kW, kH); // The total width of the scrolling View
ScrollView. showsHorizontalScrollIndicator = NO; // progress bar NO at the bottom -- hide YES --- display
ScrollView. pagingEnabled = YES; // slide the width of the entire Screen
[Self. viewaddSubview: scrollView];
}
# Pragma mark-UIScrollView proxy method implementation
-(Void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView
{
If (scrollView. contentOffset. x/kW = kCount + 1)
{
NSLog (@ "===="); // test code
ScrollView. contentOffset = CGPointMake (kW, 0 );
}
Else if (scrollView. contentOffset. x = 0)
{
ScrollView. contentOffset = CGPointMake (kW * kCount, 0 );
}
// If (scrollView. contentOffset. x> (kCount + 1) * kW)
//{
// ScrollView. contentOffset = CGPointMake (kW, 0 );
//}
// Else if (scrollView. contentOffset. x/kW = 0)
//{
// ScrollView. contentOffset = CGPointMake (kW * kCount, 0 );
//}
}
@ End
Disadvantages: reuse is not implemented, too much memory is consumed, and the optimized program will continue to be uploaded after modification. Please wait ..... PS: I feel like I cannot find any direction recently, but I am still sticking to it. I hope I can reach the end. Come on, friends!