無限滾動視圖

//

//? YZScro.m

//? scrollViewTest

//

//? Created by yuzhong.jing on 16/4/27.

//? Copyright ? 2016年 yuzhong.jing. All rights reserved.

//

#import "YZScro.h"

#define kHieght? self.frame.size.height

#define kWidth? self.frame.size.width

@implementation YZScro

{

UIScrollView *_myScroll;

UIImageView? *_leftImageView;

UIImageView? *_centerImageView;

UIImageView? *_rightimageView;

UIPageControl *_pageControl;

NSTimer? ? ? *_myTimer;

NSArray? ? ? *_myImageArray;//圖片arr

NSInteger? ? _currentIndex;//當(dāng)前

NSInteger? ? _maxImageCount;//最大

}

-(instancetype)initWithFrame:(CGRect)frame withImageArr:(NSArray *)imageArr{//初始化方法,圖片數(shù)量大于2

if (imageArr.count<2) {

return nil;

}

if (self =[super initWithFrame:frame]) {

//創(chuàng)建scroll

[self createScrollView];

_myImageArray = imageArr;

[self setMaxImageViewCount:_myImageArray.count];

}

return self;

}

- (void)setMaxImageViewCount:(NSInteger)maxCount{

_maxImageCount =maxCount;

//創(chuàng)建imageView

[self createImageViewWithImageName];

//創(chuàng)建pageControl

[self createPageControlWithPahe];

//開啟定時器

[self createTimer];

[self changeImageLeft:_maxImageCount-1 center:0 right:1];

}

- (void)createScrollView{

UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:self.bounds];

[self addSubview:scrollView];

_myScroll =scrollView;

_myScroll.backgroundColor =[UIColor clearColor];

_myScroll.pagingEnabled =YES;

_myScroll.showsHorizontalScrollIndicator =NO;

_myScroll.delegate =self;

_myScroll.contentSize =CGSizeMake(kWidth*3, 0);

_currentIndex =0;

}

- (void)createImageViewWithImageName{

UIImageView *left =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHieght)];

UIImageView *center = [[UIImageView alloc]initWithFrame:CGRectMake(kWidth, 0, kWidth, kHieght)];

center.userInteractionEnabled =YES;

UIImageView *right =[[UIImageView alloc]initWithFrame:CGRectMake(kWidth*2, 0, kWidth, kHieght)];

UITapGestureRecognizer *tapCenter =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCenter)];

[center addGestureRecognizer:tapCenter];

[_myScroll addSubview:left];

[_myScroll addSubview:center];

[_myScroll addSubview:right];

_leftImageView =left;

_centerImageView =center;

_rightimageView =right;

}

- (void)tapCenter{

NSLog(@"%@",_myImageArray[_currentIndex]);

}

- (void)createPageControlWithPahe{

UIPageControl *control =[[UIPageControl alloc]initWithFrame:CGRectMake(kWidth/2-40, 190, 80, 20)];

//? ? control.backgroundColor =[UIColor yellowColor];

_pageControl =control;

_pageControl.numberOfPages =_maxImageCount;

_pageControl.currentPage =0;

[self addSubview:_pageControl];

}

- (void)createTimer{

_myTimer =[NSTimer timerWithTimeInterval:2 target:self selector:@selector(scroll) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop]addTimer:_myTimer forMode:NSRunLoopCommonModes];

}

- (void)scroll{

NSLog(@"開啟");

[_myScroll setContentOffset:CGPointMake(_myScroll.contentOffset.x+kWidth, 0) animated:YES];

}

- (void)endScroll{

if (_myTimer == nil) {

return;

}

[_myTimer invalidate];

_myTimer=nil;

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

[self createTimer];

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

[self endScroll];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

[self changeImageWithOffset:scrollView.contentOffset.x];

}

- (void)changeImageWithOffset:(CGFloat)offsetX{

if (offsetX >= kWidth * 2) {

_currentIndex++;

if (_currentIndex == _maxImageCount-1) {

[self changeImageLeft:_currentIndex-1 center:_currentIndex right:0];

}else if (_currentIndex == _maxImageCount) {

_currentIndex = 0;

[self changeImageLeft:_maxImageCount-1 center:0 right:1];

}else {

[self changeImageLeft:_currentIndex-1 center:_currentIndex right:_currentIndex+1];

}

_pageControl.currentPage = _currentIndex;

}

if (offsetX <= 0) {

_currentIndex--;

if (_currentIndex == 0) {

[self changeImageLeft:_maxImageCount-1 center:0 right:1];

}else if (_currentIndex == -1) {

_currentIndex = _maxImageCount-1;

[self changeImageLeft:_currentIndex-1 center:_currentIndex right:0];

}else {

[self changeImageLeft:_currentIndex-1 center:_currentIndex right:_currentIndex+1];

}

_pageControl.currentPage = _currentIndex;

}

}

- (void)changeImageLeft:(NSInteger)leftIndex center:(NSInteger)centerIndex right:(NSInteger)rightIndex{

_leftImageView.image = [UIImage imageNamed:_myImageArray[leftIndex]];

_centerImageView.image = [UIImage imageNamed:_myImageArray[centerIndex]];

_rightimageView.image = [UIImage imageNamed:_myImageArray[rightIndex]];

[_myScroll setContentOffset:CGPointMake(kWidth, 0)];

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末忆矛,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子土榴,更是在濱河造成了極大的恐慌糖权,老刑警劉巖撑螺,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纺念,死亡現(xiàn)場離奇詭異苫耸,居然都是意外死亡疫鹊,警方通過查閱死者的電腦和手機(jī)咬摇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門伐蒂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人菲嘴,你說我怎么就攤上這事饿自。” “怎么了龄坪?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵昭雌,是天一觀的道長。 經(jīng)常有香客問我健田,道長烛卧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任妓局,我火速辦了婚禮总放,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘好爬。我一直安慰自己局雄,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布存炮。 她就那樣靜靜地躺著炬搭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪穆桂。 梳的紋絲不亂的頭發(fā)上宫盔,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天,我揣著相機(jī)與錄音享完,去河邊找鬼灼芭。 笑死,一個胖子當(dāng)著我的面吹牛般又,可吹牛的內(nèi)容都是我干的彼绷。 我是一名探鬼主播巍佑,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼苛预!你這毒婦竟也來了句狼?” 一聲冷哼從身側(cè)響起笋熬,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤热某,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后胳螟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體昔馋,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年糖耸,在試婚紗的時候發(fā)現(xiàn)自己被綠了秘遏。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡嘉竟,死狀恐怖邦危,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情舍扰,我是刑警寧澤倦蚪,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布,位于F島的核電站边苹,受9級特大地震影響陵且,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜个束,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一慕购、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧茬底,春花似錦沪悲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至捶枢,卻和暖如春握截,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背烂叔。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工谨胞, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蒜鸡。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓胯努,卻偏偏與公主長得像牢裳,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子叶沛,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容