iOS實(shí)現(xiàn)輪播圖banner示例
樓主項(xiàng)目中需要有一個(gè)輪播圖,因?yàn)楸容^簡(jiǎn)單,就自己寫了個(gè),因?yàn)槭菑木W(wǎng)上弄得圖片 所以用了SDWebImage 這個(gè)三方庫(kù) 當(dāng)然自己也可以去掉
類型后面有*號(hào) 如用使用 請(qǐ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ù)組過(guò)來(lái) //參數(shù)一:frame //參數(shù)二:裝有圖片名字的數(shù)組 //參數(shù)三:BOOL如果是YES,那么自動(dòng)滾動(dòng),如果是NO不滾動(dòng) -(id)initWithFrame:(CGRect)frame andImageNameArray: (NSMutableArray * )imageNameArray andIsRunning:(BOOL)isRunning; @end
.m文件
@interface TJXView()<UIScrollViewDelegate> { NSInteger _currentPage; //記錄真實(shí)的頁(yè)碼數(shù) NSTimer *_timer; //生命一個(gè)全局變量 } @property (nonatomic,assign) BOOL isRun; @property (nonatomic,strong) NSMutableArray *imageArray;//存儲(chǔ)圖片的名字 @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) { //動(dòng)畫完成的block _scrollView.contentOffset = CGPointMake(_width, 0); CGPoint realEnd = _scrollView.contentOffset; //取一遍頁(yè)碼數(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; //取一遍頁(yè)碼數(shù) _currentPage = realEnd.x/_width; _pageControl.currentPage = _currentPage-1; } } //創(chuàng)建頁(yè)碼指示器 -(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)建滾動(dò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; //分頁(yè)效果 _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(@"沒(méi)有設(shè)置代理或者沒(méi)有事先協(xié)議的方法"); } } #pragma mark UIScrollViewDelegate //停止?jié)L動(dòng) -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (_timer) { [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]]; } //圖片的個(gè)數(shù) 1 2 3 4 5 6 7 8 //真實(shí)的頁(yè)碼 0 1 2 3 4 5 6 7 //顯示的頁(yè)碼 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); } //取一遍頁(yè)碼數(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); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)毛玻璃效果(無(wú)需要第三方)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)毛玻璃效果,無(wú)需要第三方,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05iOS開發(fā)教程之識(shí)別圖片中二維碼功能的實(shí)現(xiàn)
長(zhǎng)按識(shí)別二維碼這個(gè)功能相信對(duì)大家來(lái)說(shuō)都不陌生,最近工作中就遇到了這個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于利用iOS識(shí)別圖片中二維碼的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07iOS開發(fā)中如何優(yōu)雅的調(diào)試數(shù)據(jù)庫(kù)詳解
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)中如何優(yōu)雅的調(diào)試數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12HTTP/2 協(xié)議用于 iOS 推送提醒服務(wù) (APNS)
基于JSON的請(qǐng)求和響應(yīng)對(duì)于每個(gè)通知,如果成功響應(yīng),將會(huì)返回200標(biāo)識(shí) - 不用再去猜測(cè)通知是否被接收到響應(yīng)錯(cuò)誤將會(huì)以JSON字符消息的長(zhǎng)度從2048個(gè)字節(jié)增加到4096個(gè)字節(jié)連接狀態(tài)可以通過(guò)HTTP/2的ping框架來(lái)進(jìn)行檢查.2016-04-04IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView
這篇文章主要為大家介紹了IOS簡(jiǎn)單實(shí)現(xiàn)瀑布流UICollectionView的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01