//
Rootviewcontroller.m
Head Scrolling Display view
Head scrolling AD View
#define Screen_size [UIScreen mainscreen].bounds.size
#define KIMAGECNT 5
#define KIMAGE_H 250
#import "RootViewController.h"
@interface Rootviewcontroller () <UIScrollViewDelegate>
@property (nonatomic, Strong) Uiscrollview * ScrollView;
@property (nonatomic, Strong) Uipagecontrol * Pagecontrol;
@property (nonatomic, strong) Nstimer * timer;
@end
@implementation Rootviewcontroller
/**
* ScrollView
*/
-(Uiscrollview *) ScrollView
{
if (_scrollview = = nil) {
_scrollview = [[Uiscrollview alloc] Initwithframe:cgrectmake (0, 0, Screen_size.width, Kimage_h)];
_scrollview.pagingenabled = YES;
_scrollview.showshorizontalscrollindicator = NO;
}
return _scrollview;
}
/**
* Pagecontrol
*/
-(Uipagecontrol *) Pagecontrol
{
if (_pagecontrol = = nil) {
_pagecontrol = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0, self.scrollview.frame.size.height-30, SCREEN_ Size.width, 20)];
_pagecontrol.currentpageindicatortintcolor = [Uicolor colorwithred:65/255.0 green:168/255.0 blue:100/255.0 alpha:1.0 ];
_pagecontrol.pageindicatortintcolor = [Uicolor Graycolor];
}
return _pagecontrol;
}
/**
* Initialize UI
*/
-(void) setupui
{
[Self.view AddSubview:self.scrollView];
[Self.view AddSubview:self.pageControl];
CGFloat imagey = 0;
CGFloat Imagew = screen_size.width;
CGFloat Imageh = Kimage_h;
for (int i = 0; i < kimagecnt; i++) {
Uiimageview * ImageView = [[Uiimageview alloc] Initwithframe:cgrectmake (i * imagew, Imagey, Imagew, Imageh)];
Imageview.image = [UIImage imagenamed:[nsstring stringwithformat:@ "Image_%i.jpg", i+1]];
[Self.scrollview Addsubview:imageview];
}
Setting the ScrollView Property
Self.scrollView.contentSize = Cgsizemake (kimagecnt * imagew, 0);
Self.scrollView.delegate = self;
Self.pageControl.numberOfPages = kimagecnt;
Set auto Play
[Self turnontimer];
}
/**
* Turn on the timer
*/
-(void) Turnontimer
{
Self.timer = [Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (playimage) Userinfo:nil Repeats:yes];
To prevent single-threaded defects
[[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];
}
/**
* Turn off the timer
*/
-(void) Turnofftimer
{
[Self.timer invalidate];
Self.timer = nil;
}
/**
* Automatically play pictures
*/
-(void) playimage
{
int i = Self.pageControl.currentPage;
if (i = = KImageCnt-1) {
i =-1;
}
i++;
[Self.scrollview setcontentoffset:cgpointmake (i * self.scrollView.frame.size.width, 0) Animated:yes];
}
-(void) Viewdidload {
[Super Viewdidload];
[Self setupui];
}
#pragma mark-uiscrollviewdelegate
/**
* Close Timer when user is ready to drag and drop
*/
-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView
{
[Self turnofftimer];
}
/**
* When the user stops dragging, a new timer is opened.
*/
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate
{
[Self turnontimer];
}
Determines the scrolling position when the timer is scrolled so that Pagecontrol displays the current page
The width of a half ScrollView is added to the total width to achieve the effect of dragging to the left half of the time
-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView
{
Self.pageControl.currentPage = (Self.scrollView.frame.size.width * 0.5 + self.scrollview.contentoffset.x)/ Self.scrollView.frame.size.width;
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
}
@end
IOS Development Head Scrolling display view (GO)