1. Create a container view
Uiscrollview * ScrollView = [[Uiscrollview alloc] init];
[Self.view Addsubview:scrollview];
Scrollview.frame = CGRectMake (0, 0, self.view.frame.size.width, 200);
Scrollview.backgroundcolor = [Uicolor Yellowcolor];
Self.scrollview = ScrollView;
2. Add a content sub-view
for (int i = 0; i < 5; i++)
{
Uiimageview * Pictimageview = [[Uiimageview alloc] init];
[ScrollView Addsubview:pictimageview];
NSString * name = [NSString stringwithformat:@ "ad_%02d", I];
Pictimageview.image = [UIImage imagenamed:name];
Set frame
CGFloat pictw = scrollView.frame.size.width;
CGFloat picth = scrollView.frame.size.height;
CGFloat pictx = i * pictw;//i * scrollView.frame.size.width
CGFloat picty = 0;
Pictimageview.frame = CGRectMake (Pictx, Picty, PICTW, PICTH);
}//end for
Set Contentsize to scroll horizontally only
Scrollview.contentsize = Cgsizemake (5 * scrollView.frame.size.width, 0);
Scrollview.showshorizontalscrollindicator = NO;
Whether to support paging feature
scrollview.pagingenabled = YES;
indicator that prompts the user for a specific page hint that is currently displayed
Uipagecontrol * Pagecontroller = [[Uipagecontrol alloc] init];
[Self.view Addsubview:pagecontroller];
Pagecontroller.frame = CGRectMake (0, N. scrollView.frame.size.width, 20);
Pagecontroller.backgroundcolor = [Uicolor Redcolor];
Set how many hint views (small dots) are in total
Pagecontroller.numberofpages = 5;
The dot color of the current indicator page
Pagecontroller.currentpageindicatortintcolor = [Uicolor Greencolor];
Other small dot Color
Pagecontroller.pageindicatortintcolor = [Uicolor Graycolor];
Current instruction on the first few dots
pagecontroller.currentpage = 0;
-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView
{
1. Always get current offset
CGFloat OffSetX = scrollview.contentoffset.x;
Calculates the number of pages currently scrolled to
Nsinteger currentpage = (OffSetX + scrollView.frame.size.width * 0.5)/scrollView.frame.size.width;
2. Set Pegecontroler
Self.pageController.currentPage = CurrentPage;
}
To prevent the screen view from flashing when the cumulative effect of the time accumulated by the Nstimer, we cancel the auto-scrolling effect when the human is dragged.
-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView
{
Cancels the timer function, once the Invalidate method is called, the timer object is not able to revive again
[Self.timer invalidate];
Clear the Timer object
Self.timer = nil;
}
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate
{
Restart Timer Object
[Self.timer fire]; It is useless to call this method, and once the call to invalidate is not able to resurrect again, only be able to recreate
[Self createtimer];
}
-(void) Createtimer
{
Nstimer * timer = [Nstimer scheduledtimerwithtimeinterval:2 target:self selector: @selector (AutoScroll) Userinfo:nil Repeats:yes];
Self.timer = timer;
Add Timer object to loop loop, Nsrunloopcommonmodes communication mode
[[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
}
-(void) AutoScroll
{
NSLog (@ "start scrolling automatically");
/*
The ScrollView scrolling effect is actually changing the value of the bounds, which is changing the contentoffset direction.
Currently displayed on page *scrollview.width
If it has been shown to the last page, then return to 0
*/
Nsinteger currentpage = Self.pageController.currentPage >= 4? 0:self.pagecontroller.currentpage + 1;
CGFloat OffsetX = currentpage * self.scrollView.frame.size.width;
Self.scrollView.contentOffset = Cgpointmake (OffsetX, 0);
}
Advertising Carousel Source