Scroll through the scrollView loop --- the last image is followed by the first image. The first image is followed by the last image.

Source: Internet
Author: User

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!



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.