最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實現(xiàn)輪播圖banner示例

 更新時間:2017年01月22日 08:57:31   作者:字母大師  
本篇文章主要介紹了iOS實現(xiàn)輪播圖banner示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

樓主項目中需要有一個輪播圖,因為比較簡單,就自己寫了個,因為是從網(wǎng)上弄得圖片 所以用了SDWebImage 這個三方庫 當(dāng)然自己也可以去掉

類型后面有*號 如用使用 請自行加上。。。。。

代碼:.h 文件

@protocol TJXViewDelegate<NSObject>
//判斷點擊的那個
-(void)sendImageName:(TJXView *)TJXView andName:(NSInteger)selectImage;
@end
@interface TJXView : UIView
@property (nonatomic,weak)id<TJXViewDelegate>delegate;
//傳一個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ù)
  NSTimer *_timer; //生命一個全局變量
}
@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)容放到一個數(shù)組中返回
    self.imageArray = [NSMutableArray arrayWithArray:imageNameArray];
    //在數(shù)組的尾部添加原數(shù)組第一個元素
    [self.imageArray addObject:[imageNameArray firstObject]];
    //在數(shù)組的首部添加原數(shù)組最后一個元素
    [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)前的點
  CGPoint point = _scrollView.contentOffset;
  //2求得將要變換的點
  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中更改一個圖片
    [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]];
  }
  //圖片的個數(shù) 1 2 3 4 5 6 7 8
  //真實的頁碼 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;
}
//手指開始觸摸的時候,停止計時器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  if (_timer) {
    //如果有,停掉
    [_timer setFireDate:[NSDate distantFuture]];
  }
}

在項目中  導(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);
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線

    iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線

    本篇文章主要介紹了iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線,非常具有實用價值,需要的朋友可以參考下
    2017-05-05
  • iOS微信支付開發(fā)案例

    iOS微信支付開發(fā)案例

    這篇文章主要為大家詳細(xì)介紹了iOS微信支付開發(fā)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • iOS NSTimer循環(huán)引用的幾種解決辦法

    iOS NSTimer循環(huán)引用的幾種解決辦法

    本篇文章主要介紹了iOS NSTimer循環(huán)引用的幾種解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 淺析iOS中視頻播放的幾種方案

    淺析iOS中視頻播放的幾種方案

    還記得剛學(xué)iOS的時候嗎?那個時候驚訝于各種牛逼的功能只需要幾句簡單的代碼就可以完成。視頻播放也是這樣,IOS中視頻播放有好幾種方式,這篇文章就給大家整理這幾種方案優(yōu)缺點與實現(xiàn)過程。
    2016-08-08
  • iOS新增繪制圓的方法實例代碼

    iOS新增繪制圓的方法實例代碼

    這篇文章主要給大家介紹了關(guān)于iOS新增繪制圓的方法,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • iOS CAReplicatorLayer實現(xiàn)脈沖動畫效果

    iOS CAReplicatorLayer實現(xiàn)脈沖動畫效果

    這篇文章主要介紹了iOS CAReplicatorLayer實現(xiàn)脈沖動畫效果 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • iOS如何跳轉(zhuǎn)到App Store下載評分頁面示例代碼

    iOS如何跳轉(zhuǎn)到App Store下載評分頁面示例代碼

    最近在工作中遇到一個需求,需要跳轉(zhuǎn)到App Store下載評分,通過查找相關(guān)的資料最終解決了,下面這篇文章主要給大家介紹了關(guān)于iOS如何跳轉(zhuǎn)到App Store下載評分頁面的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • iOS實現(xiàn)毫秒倒計時的方法詳解

    iOS實現(xiàn)毫秒倒計時的方法詳解

    倒計時在我們?nèi)粘i_發(fā)中必不可少,最近在公司的一個項目中就遇到了這個需求,本文著重介紹的是利用iOS實現(xiàn)毫秒倒計時的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04
  • Objective-C方法的聲明實現(xiàn)及調(diào)用方法

    Objective-C方法的聲明實現(xiàn)及調(diào)用方法

    這篇文章主要介紹了Objective-C方法的聲明實現(xiàn)及調(diào)用方法,包括五參數(shù)的方法和單個參數(shù)的方法,結(jié)合實例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-02-02
  • 實例解析iOS app開發(fā)中音頻文件播放工具類的封裝

    實例解析iOS app開發(fā)中音頻文件播放工具類的封裝

    這篇文章主要介紹了iOS app開發(fā)中音頻文件播放工具類的封裝,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-01-01

最新評論

伊川县| 屯留县| 弥渡县| 宣恩县| 洛南县| 固阳县| 汝阳县| 吉安县| 丰都县| 叙永县| 堆龙德庆县| 万全县| 阜新| 河津市| 河北省| 西宁市| 肃北| 井陉县| 华坪县| 济阳县| 任丘市| 永宁县| 新津县| 通榆县| 客服| 名山县| 镇安县| 庐江县| 禄劝| 桐梓县| 枣阳市| 乌拉特中旗| 阜平县| 田林县| 卢龙县| 封丘县| 中卫市| 固镇县| 彰化市| 大安市| 长葛市|