欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Objective-C實現(xiàn)無限循環(huán)輪播器

 更新時間:2016年05月19日 16:05:14   作者:zhangmi  
這篇文章主要介紹了Objective-C實現(xiàn)無限循環(huán)輪播器的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

先看看效果圖:

具體實現(xiàn)代碼:

1. 控制器    

//
// AppDelegate.m
// 無限輪播器
//
// Created by zhangmi on 16/5/16.
// Copyright © 2016年 Paramount Pictures. All rights reserved.
//
#import "ViewController.h"
#import "SNInfiniteScrollView.h"
 
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
  
 NSMutableArray * images = [NSMutableArray array];
 for (int i = 0; i < 5; i++) {
  NSString * imageName = [NSString stringWithFormat:@"ad_%02d", i]; //img01
  UIImage * image = [UIImage imageNamed:imageName];
  [images addObject:image];
 }
 UIView * scrollView = [SNInfiniteScrollView scrollViewWithFrame:CGRectMake(0, 20, 414, 200) superView:self.view images:images scrollDirection:ScrollDirectionHorizontal pageIndicatorTintColor:[UIColor lightGrayColor] currentPageIndicatorTintColor:[UIColor orangeColor] imageViewcontentMode:UIViewContentModeScaleAspectFit];
  
 [self.view addSubview:scrollView];
}
 
@end

2. 顯示內容界面設置

//
// AppDelegate.m
// 無限輪播器
//
// Created by zhangmi on 16/5/16.
// Copyright © 2016年 Paramount Pictures. All rights reserved.
//
 
#import "SNInfiniteScrollView.h"
 
static int const ImageViewCount = 3;
#define scrollViewWidth self.scrollView.frame.size.width
#define scrollViewHeight self.scrollView.frame.size.height
 
@interface SNInfiniteScrollView () <UIScrollViewDelegate>
 
@property(weak, nonatomic) UIScrollView * scrollView;
@property(weak, nonatomic) NSTimer * timer;
/** pageIndex */
@property(nonatomic, assign) NSInteger pageIndex;
 
@end
 
@implementation SNInfiniteScrollView
 
- (void)setImages:(NSArray<UIImage *> *)images {
  
 _images = images;
  
 // 設置頁碼
 self.pageIndex = 0;
  
 // 設置內容
 [self updateContent];
  
 // 開始定時器
 [self startTimer];
}
 
/** 代碼創(chuàng)建的時候調用. */
- (instancetype)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {
  // 滾動視圖
  UIScrollView * scrollView = [[UIScrollView alloc] init];
   
  self.scrollView = scrollView;
  scrollView.delegate = self;
  // scroller屬性
  scrollView.showsHorizontalScrollIndicator = NO;
  scrollView.showsVerticalScrollIndicator = NO;
  scrollView.pagingEnabled = YES;
  scrollView.bounces = NO;
  // 添加scrollView
  [self addSubview:scrollView];
   
  // 圖片控件
  for (int i = 0; i < ImageViewCount; i++) {
   UIImageView * imageView = [[UIImageView alloc] init];
   // 圖片不變形處理.
   imageView.contentMode = self.imageViewcontentMode;
   [scrollView addSubview:imageView];
  }
 }
 return self;
}
/** 布局 子控件, 只執(zhí)行一次 */
- (void)layoutSubviews {
 [super layoutSubviews];
  
 self.scrollView.frame = self.bounds;
 if (self.scrollDirection == ScrollDirectionVertical) {
  self.scrollView.contentSize = CGSizeMake(0, ImageViewCount * self.bounds.size.height);
 } else {
  self.scrollView.contentSize = CGSizeMake(ImageViewCount * self.bounds.size.width, 0);
 }
  
 for (int i = 0; i < ImageViewCount; i++) {
  UIImageView * imageView = self.scrollView.subviews[i];
   
  if (self.scrollDirection == ScrollDirectionVertical) {
   imageView.frame = CGRectMake(0, i * self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
  } else {
   imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
  }
 }
 // 設置內容
 [self updateContent];
}
#pragma mark - 內容更新
- (void)updateContent {
 // 設置圖片
 for (int i = 0; i < self.scrollView.subviews.count; i++) {
   
  NSInteger pageIndex = self.pageIndex;
  // 遍歷每一個imageView
  UIImageView * imageView = self.scrollView.subviews[i];
   
  if (i == 0) {
   pageIndex--;
  } else if (i == 2) {
   pageIndex++;
  }
   
  if (pageIndex < 0) {
   pageIndex = self.images.count - 1;
  } else if (pageIndex >= self.images.count) {
   pageIndex = 0;
  }
  // 圖片角標 賦值給 imageView的tag
  imageView.tag = pageIndex;
  imageView.image = self.images[imageView.tag];
 }
  
 // 設置偏移量在中間 // 不能使用帶動畫 contentOffset
 if (self.scrollDirection == ScrollDirectionVertical) {
  self.scrollView.contentOffset = CGPointMake(0, scrollViewHeight);
 } else {
  self.scrollView.contentOffset = CGPointMake(scrollViewWidth, 0);
 }
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
 // 找出最中間的那個圖片控件
 NSInteger page = self.pageIndex;
 CGPoint point = CGPointZero;
 for (int i = 0; i < self.scrollView.subviews.count; i++) {
  UIImageView * imageView = self.scrollView.subviews[i];
  point = [scrollView convertPoint:imageView.frame.origin toView:self.superview];
  //=****** other way ****************** stone ***
  if (self.scrollDirection == ScrollDirectionVertical) {
   if (ABS(point.y - self.frame.origin.y) < 1.0) {
    page = imageView.tag;
   }
  } else {
   if (ABS(point.x - self.frame.origin.x) < 1.0) {
    page = imageView.tag;
   }
  }
 }
 self.pageIndex = page;
 self.pageControl.currentPage = page;
  
 //拖動結束會調用 [self updateContent];
 //#warning mark - 沒有動畫正常 , 有動畫不動, 一直是原點
 // [self updateContent]; // 沒有動畫正常 , 有動畫不動, 一直是原點
}
/** 開始拖拽 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
 // 停止定時器
 [self stopTimer];
}
/** 結束拖拽 */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
 // 開啟定時器
 [self startTimer];
}
/** 減速完畢 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
 // 更新內容 , 如果contentOffset 不帶動畫的話 不走這個方法
 [self updateContent];
}
/** 結束滾動動畫 */ // 這是保險的做法吧... 如果contentOffset 不帶動畫的話 不走這個方法
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
 // 更新內容
 [self updateContent];
}
 
#pragma mark - 定時器處理
- (void)startTimer {
  
 NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(next:) userInfo:nil repeats:YES];
 // [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
 [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
 self.timer = timer;
}
 
- (void)stopTimer {
 [self.timer invalidate];
 self.timer = nil;
}
 
- (void)next:(NSTimer *)timer {
 if (self.scrollDirection == ScrollDirectionVertical) {
  [self.scrollView setContentOffset:CGPointMake(0, 2 * self.scrollView.frame.size.height) animated:YES];
 } else {
  [self.scrollView setContentOffset:CGPointMake(2 * self.scrollView.frame.size.width, 0) animated:YES];
 }
}
//=****** 簡單調用 ****************** stone ***
+ (instancetype)scrollViewWithFrame:(CGRect)frame superView:(UIView *)superView images:(NSArray<UIImage *> *)images scrollDirection:(ScrollDirection)scrollDirection pageIndicatorTintColor:(UIColor *)pageIndicatorTintColor currentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor imageViewcontentMode:(UIViewContentMode)imageViewcontentMode {
  
 //=****** 添加自定義scrollView ****************** stone ***
 SNInfiniteScrollView * scrollView = [[SNInfiniteScrollView alloc] init];
 scrollView.frame = frame;
 scrollView.imageViewcontentMode = imageViewcontentMode;
 scrollView.scrollDirection = scrollDirection;
 //=****** 添加image ****************** stone ***
 scrollView.images = images;
 //=****** 添加pageControl ****************** stone ***
 UIPageControl * pageControl = [[UIPageControl alloc] init];
 scrollView.pageControl = pageControl;
 pageControl.enabled = NO;
 pageControl.currentPageIndicatorTintColor = currentPageIndicatorTintColor;
 pageControl.pageIndicatorTintColor = pageIndicatorTintColor;
 pageControl.numberOfPages = scrollView.images.count;
 pageControl.bounds = CGRectMake(0, 0, scrollView.bounds.size.width, 44);
 pageControl.center = CGPointMake(scrollView.bounds.size.width * 0.5, scrollView.bounds.size.height * 0.9);
 [scrollView addSubview:pageControl];
 [superView addSubview:scrollView];
 //=************************ stone ***
 return scrollView;
}
 
@end

以上就是本文的全部內容,希望對大家的學習有所幫助。

相關文章

  • IOS MenuViewController實現(xiàn)彈出菜單效果

    IOS MenuViewController實現(xiàn)彈出菜單效果

    這篇文章主要介紹了IOS MenuViewController實現(xiàn)彈出菜單效果,實現(xiàn)一個從下移動上來的彈出菜單,背景逐漸變深,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 詳解iOS使用Keychain中的kSecClassGenericPassword存儲數(shù)據(jù)

    詳解iOS使用Keychain中的kSecClassGenericPassword存儲數(shù)據(jù)

    iOS設備中的Keychain是一個安全的存儲容器,本篇文章主要介紹了iOS使用Keychain中的kSecClassGenericPassword存儲數(shù)據(jù),有興趣的可以了解一下。
    2016-11-11
  • 詳解iOS本地推送與遠程推送

    詳解iOS本地推送與遠程推送

    這篇文章主要為大家詳細介紹了iOS本地推送與遠程推送,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • IOS中的七種手勢小結

    IOS中的七種手勢小結

    本文給大家總結了ios中的七種手勢,手勢在開發(fā)在開發(fā)中經(jīng)常會用到,今天小編通過本文給大家詳細介紹下,需要的朋友參考下
    2016-11-11
  • 手把手教你實現(xiàn)微信小視頻iOS代碼實現(xiàn)

    手把手教你實現(xiàn)微信小視頻iOS代碼實現(xiàn)

    這篇文章主要手把手教你實現(xiàn)微信小視頻,iOS代碼實現(xiàn)微信小視頻功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • iOS利用AFNetworking3.0——實現(xiàn)文件斷點下載

    iOS利用AFNetworking3.0——實現(xiàn)文件斷點下載

    這篇文章主要介紹了iOS利用AFNetworking3.0——實現(xiàn)文件斷點下載,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • iOS實現(xiàn)屏幕亮度和閃光燈控制的實例代碼

    iOS實現(xiàn)屏幕亮度和閃光燈控制的實例代碼

    本篇文章主要介紹了iOS實現(xiàn)屏幕亮度和閃光燈控制的實例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-06-06
  • iOS設置圓角的三種方式

    iOS設置圓角的三種方式

    本文給大家分享ios設置圓角的三種方式,相對來說最簡單的一種是第一種方法,具體內容詳情參考下本文
    2017-03-03
  • iOS中常見的視圖和圖片處理示例詳解

    iOS中常見的視圖和圖片處理示例詳解

    在日常ios開發(fā)中經(jīng)常會遇到視圖和圖片的處理,下面這篇文章主要給大家總結介紹了關于iOS中常見的視圖和圖片處理的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習和工作具有一定的參考學習價值,需要的朋友可以參考下。
    2017-10-10
  • iOS 設置狀態(tài)欄的背景顏色方法

    iOS 設置狀態(tài)欄的背景顏色方法

    下面小編就為大家?guī)硪黄猧OS 設置狀態(tài)欄的背景顏色方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04

最新評論