iOS 輪播圖 banner

樓主項(xiàng)目中需要有一個(gè)輪播圖邢笙,因?yàn)楸容^簡單,就自己寫了個(gè)淤齐,因?yàn)槭菑木W(wǎng)上弄得圖片 所以用了SDWebImage 這個(gè)三方庫 當(dāng)然自己也可以去掉

類型后面有*號 如用使用 請自行加上歉铝。。碑诉。。侥锦。

代碼:进栽。h 文件


@protocol TJXViewDelegate<NSObject>
//判斷點(diǎn)擊的那個(gè)
-(void)sendImageName:(TJXView *)TJXView andName:(NSInteger)selectImage;
@end
@interface TJXView : UIView
@property (nonatomic,weak)id<TJXViewDelegate>delegate;
//傳一個(gè)frame 和 裝有圖片名字的數(shù)組過來
//參數(shù)一:frame
//參數(shù)二:裝有圖片名字的數(shù)組
//參數(shù)三:BOOL如果是YES,那么自動滾動恭垦,如果是NO不滾動
-(id)initWithFrame:(CGRect)frame andImageNameArray:
(NSMutableArray * )imageNameArray andIsRunning:(BOOL)isRunning;
@end
.m文件

@interface TJXView()<UIScrollViewDelegate>
{
    NSInteger _currentPage; //記錄真實(shí)的頁碼數(shù)
    NSTimer *_timer;  //生命一個(gè)全局變量
}
@property (nonatomic,assign) BOOL isRun;
@property (nonatomic,strong) NSMutableArray *imageArray;//存儲圖片的名字
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,assign) CGFloat width;//view的寬
@property (nonatomic,assign) CGFloat height;//view的高
@end

快毛、格嗅、、

-(id)initWithFrame:(CGRect)frame andImageNameArray:(NSMutableArray *)imageNameArray andIsRunning:(BOOL)isRunning{
    self = [super initWithFrame:frame];
    if (self) {
        _width = self.frame.size.width;
        _height = self.frame.size.height;
        //arrayWithArray 把數(shù)組中的內(nèi)容放到一個(gè)數(shù)組中返回
        self.imageArray = [NSMutableArray arrayWithArray:imageNameArray];
        //在數(shù)組的尾部添加原數(shù)組第一個(gè)元素
        [self.imageArray addObject:[imageNameArray firstObject]];
        //在數(shù)組的首部添加原數(shù)組最后一個(gè)元素
        [self.imageArray insertObject:[imageNameArray lastObject] atIndex:0];
        self.isRun = isRunning;
        _currentPage = 0;
        [self createSro];
        [self createPageControl];
        [self createTimer];
    }
    return self;
}

唠帝、屯掖、、
-(void)createTimer{
    if (_isRun == YES) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(change) userInfo:nil repeats:YES ];
        [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];    }
}
-(void)change{
    //1獲得當(dāng)前的點(diǎn)
    CGPoint point = _scrollView.contentOffset;
    //2求得將要變換的點(diǎn)
    CGPoint endPoint = CGPointMake(point.x+_width, 0);
    //判斷
    if (endPoint.x == (self.imageArray.count-1)*_width) {
        [UIView animateWithDuration:0.25 animations:^{
            _scrollView.contentOffset = CGPointMake(endPoint.x, 0);
        } completion:^(BOOL finished) {
            //動畫完成的block
            _scrollView.contentOffset = CGPointMake(_width, 0);
            CGPoint realEnd = _scrollView.contentOffset;
            //取一遍頁碼數(shù)
            _currentPage = realEnd.x/_width;
            _pageControl.currentPage = _currentPage-1;
        }];
    }
    else{
        //0.25s中更改一個(gè)圖片
        [UIView animateWithDuration:0.25 animations:^{
            _scrollView.contentOffset = endPoint;
        } completion:^(BOOL finished) {
        }];
               CGPoint realEnd = _scrollView.contentOffset;
        //取一遍頁碼數(shù)
        _currentPage = realEnd.x/_width;
        _pageControl.currentPage = _currentPage-1;
    }   
}
//創(chuàng)建頁碼指示器
-(void)createPageControl{
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(_width-200, _height-30, 100, 30)];
    _pageControl.centerX = _width/2;
    _pageControl.numberOfPages = self.imageArray.count-2;
    _pageControl.pageIndicatorTintColor = WP_GRAY_COLOR;
    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.userInteractionEnabled = NO;
    [self addSubview:_pageControl];
}
//創(chuàng)建滾動視圖
-(void)createSro{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _width, _height)];
    _scrollView.contentSize = CGSizeMake(_width*self.imageArray.count, _height);
    for (int i = 0; i < self.imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*_width, 0, _width, _height)];
//        imageView.image = [UIImage imageNamed:self.imageArray[i]];
        [imageView sd_setImageWithURL:self.imageArray[i] placeholderImage:[UIImage imageNamed:@"home_banner_blank"]];
        imageView.userInteractionEnabled = YES;
        imageView.tag = 200+i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [imageView addGestureRecognizer:tap];
        [_scrollView addSubview:imageView];
    }
    //水平指示條不顯示
    _scrollView.showsHorizontalScrollIndicator = NO;
    //關(guān)閉彈簧效果
    _scrollView.bounces = NO;
    //設(shè)置用戶看到第一張
    _scrollView.contentOffset = CGPointMake(_width, 0);
    //設(shè)置代理
    _scrollView.delegate = self;
    //分頁效果
    _scrollView.pagingEnabled = YES;
    [self addSubview:_scrollView];
}
-(void)tap:(UITapGestureRecognizer *)tap{
    if(_delegate&&[_delegate respondsToSelector:@selector(sendImageName:andName:)]){
        [_delegate sendImageName:self andName:tap.view.tag-201];
    }else{
        NSLog(@"沒有設(shè)置代理或者沒有事先協(xié)議的方法");
    } 
}
#pragma mark UIScrollViewDelegate
//停止?jié)L動
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (_timer) {
        [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
    }
    //圖片的個(gè)數(shù)  1 2 3 4 5 6 7 8
    //真實(shí)的頁碼  0 1 2 3 4 5 6 7
    //顯示的頁碼    0 1 2 3 4 5
    CGPoint point = _scrollView.contentOffset;
    if (point.x == (self.imageArray.count-1)*_width) {
        scrollView.contentOffset = CGPointMake(_width, 0);
    }
    if (point.x == 0) {
        scrollView.contentOffset = CGPointMake((self.imageArray.count-2)*_width, 0);
    }
    //取一遍頁碼數(shù)
    CGPoint endPoint = scrollView.contentOffset;
    _currentPage = endPoint.x/_width;
    _pageControl.currentPage = _currentPage-1;
}
//手指開始觸摸的時(shí)候襟衰,停止計(jì)時(shí)器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (_timer) {
        //如果有贴铜,停掉
        [_timer setFireDate:[NSDate distantFuture]];
    }
}
在項(xiàng)目中  導(dǎo)入頭文件  遵守代理
        TJXView * TJXView = [[TJXView alloc]initWithFrame:CGRectMake(0, 0, WPSCREEN_WIDTH, 100*WPSCREEN_HIGTH_RATIO) andImageNameArray:self.bannerImager andIsRunning:YES];
        TJXView.delegate = self;
        [self.view addSubview: TJXView];
#pragma mark TJXViewDelegate
-(void)sendImageName:(TJXView *) TJXView andName:(NSInteger)selectImage{
     KKLog(@"%ld",(long)selectImage);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市瀑晒,隨后出現(xiàn)的幾起案子绍坝,更是在濱河造成了極大的恐慌,老刑警劉巖瑰妄,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件陷嘴,死亡現(xiàn)場離奇詭異,居然都是意外死亡间坐,警方通過查閱死者的電腦和手機(jī)灾挨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來竹宋,“玉大人劳澄,你說我怎么就攤上這事◎谄撸” “怎么了秒拔?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長飒硅。 經(jīng)常有香客問我砂缩,道長,這世上最難降的妖魔是什么三娩? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任庵芭,我火速辦了婚禮,結(jié)果婚禮上雀监,老公的妹妹穿的比我還像新娘双吆。我一直安慰自己,他們只是感情好会前,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布好乐。 她就那樣靜靜地躺著,像睡著了一般瓦宜。 火紅的嫁衣襯著肌膚如雪蔚万。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天临庇,我揣著相機(jī)與錄音笛坦,去河邊找鬼区转。 笑死,一個(gè)胖子當(dāng)著我的面吹牛版扩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播侄泽,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼礁芦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了悼尾?” 一聲冷哼從身側(cè)響起柿扣,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎闺魏,沒想到半個(gè)月后未状,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡析桥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年司草,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片泡仗。...
    茶點(diǎn)故事閱讀 39,992評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡埋虹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出娩怎,到底是詐尸還是另有隱情搔课,我是刑警寧澤,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布截亦,位于F島的核電站爬泥,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏崩瓤。R本人自食惡果不足惜袍啡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望谷遂。 院中可真熱鬧葬馋,春花似錦、人聲如沸肾扰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽集晚。三九已至窗悯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間偷拔,已是汗流浹背蒋院。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工亏钩, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人欺旧。 一個(gè)月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓姑丑,卻偏偏與公主長得像,于是被迫代替她去往敵國和親辞友。 傳聞我的和親對象是個(gè)殘疾皇子栅哀,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評論 2 355

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,144評論 25 707
  • 曾經(jīng)當(dāng)了10年的家庭主婦,也許跟很多人一樣無奈也好称龙,還是家庭成員搭配也好留拾,所有媽媽都差不多,眼中全是孩子鲫尊、丈...
    蘇景燦閱讀 1,904評論 0 26
  • 少走彎路這個(gè)題目貌似和我的昵稱沖突了…… 忽略3杖帷!疫向! 今天是純文字的分享咳蔚,啦啦啦。 書籍—— 《牛奶可樂經(jīng)濟(jì)學(xué)》—...
    5070黑土閱讀 73評論 10 7
  • 北書房閱讀 622評論 7 16