iOS-圖片輪播-SDCycleSCrollView的使用

介紹: SDCycleScrollView 是一款很是強大的輪播圖第三方. 輪播流暢,手滑流暢.使用方便.自定義簡單. 能夠更改pageControl.web

一. Demo地址 https://pan.baidu.com/disk/home#list/path=%2F總結%2FDemo-圖片輪播數組

二.效果截圖.緩存

三.代碼.網絡

#import "ViewController.h"

#import "Masonry.h"

#import "SDCycleScrollView.h"

#define kWidth [UIScreen mainScreen].bounds.size.width

@interface ViewController () <SDCycleScrollViewDelegate>

@property (nonatomic, strong) UIImageView  * bgImageView;
@property (nonatomic, strong) UIScrollView * bgScrollView;

// 本地圖片
@property (nonatomic, strong) SDCycleScrollView * localCycleScrollView;

// 網絡圖片
@property (nonatomic, strong) SDCycleScrollView * webCycleScrollView;

// 自定義pageControl
@property (nonatomic, strong) SDCycleScrollView * customCycleScrollView;

// 跑馬燈效果
@property (nonatomic, strong) SDCycleScrollView * textCycleScrollView;

@end

/** 滾動控制接口
 1. autoScrollTimeInterval 自動滾動間隔,默認爲2s.
 2. infiniteLoop 是否無限循環.
 3. autoScroll 是否滾動設置.
 4. scrollDirection 圖片滾動方向.
 5. delegate 設置代理.
 6. adjustWhenControllerViewWillAppera 解決viewWillAppear時出現時輪播圖卡在一半的問題,在控制器viewWillAppear時調用此方法
 7.
 */

/** 自定義樣式接口
 1. bannerImageViewContentMode 輪播圖的ContentMode,默認爲UIViewContentModeScaleToFill
 2. placeholderImage 佔位圖 用於網絡
 3. showPageControl 是否顯示分頁控件
 4. hidesForSinglePage 是否在只有一張圖片時隱藏pageControl,默認爲Yes.
 5. onlyDisplayText 只顯示文字輪播
 6. pageControlStyle 樣式. 默認爲動畫樣式
 7. pageControlAliment 分頁控件的位置
 8. pageControlBottomOffset 分頁控件距離輪播圖的右邊間距.
 9. pageControlDotSize 分頁控件小圓標的大小
 10. currentPageDotColor當前分頁控件小圓標顏色
 11. currentPageDotImage 當前分頁控件小圓標的圖片
 12. pageDotImage 其餘分頁控件小圓標圖片
 13. titleLabelTextColor 輪播文字label字體顏色
 14. titleLabelTextFont 輪播文字label的字體大小
 15. titleLabelBackgroundColor 輪播文字label背景顏色
 16. titleLabelHeight 輪播文字字體高度
 */

/** 清除緩存
 1. clearImagesCache
 [SDCycleScrollView clearImagesCache];
 */

@implementation ViewController

#pragma mark - 生命週期
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 若是你發現你的CycleScrollview會在viewWillAppear時圖片卡在中間位置,你能夠調用此方法調整圖片位置
    //    [你的CycleScrollview adjustWhenControllerViewWillAppera];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

#pragma mark viewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self basicSetting];
    
    [self addBgModule];
    
    // 本地存儲的圖片
    [self local_storage];
    
    // 網絡圖片
    [self webImage];
    
    [self cuatomPageControl];
    
    [self onlyText];
}


#pragma mark - 系統代理
#pragma mark SDCycleScrollViewDelegate

// 點擊圖片代理方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---點擊了第%ld張圖片", (long)index);
    
    // 清理緩存
    [SDCycleScrollView clearImagesCache];

}

// 滾動到第幾張圖片的回調
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index
{
    NSLog(@">>>>>> 滾動到第%ld張圖", (long)index);
}

#pragma mark - 點擊事件

#pragma mark - 實現方法
#pragma mark 基本設置
- (void)basicSetting
{
    self.title = @"";
}

- (void)addBgModule
{
    [self.view addSubview:self.bgImageView];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.view addSubview:self.bgScrollView];
    [self.bgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)local_storage
{
    
    [self.bgScrollView addSubview:self.localCycleScrollView];
    [self.localCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.bgScrollView).with.offset(0);
        make.width.mas_equalTo(kWidth);
        make.top.mas_equalTo(self.bgScrollView).with.offset(20);
        make.height.mas_equalTo(180);
    }];
}

- (void)webImage
{
    [self.bgScrollView addSubview:self.webCycleScrollView];
    [self.webCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.bgScrollView).with.offset(0);
        make.width.mas_equalTo(kWidth);
        make.top.mas_equalTo(self.localCycleScrollView.mas_bottom).with.offset(20);
        make.height.mas_equalTo(180);
    }];
}

- (void)cuatomPageControl
{
    [self.bgScrollView addSubview:self.customCycleScrollView];
    [self.customCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.bgScrollView).with.offset(0);
        make.width.mas_equalTo(kWidth);
        make.top.mas_equalTo(self.webCycleScrollView.mas_bottom).with.offset(20);
        make.height.mas_equalTo(180);
    }];
}

- (void)onlyText
{
    [self.bgScrollView addSubview:self.textCycleScrollView];
    [self.textCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.bgScrollView).with.offset(0);
        make.width.mas_equalTo(kWidth);
        make.top.mas_equalTo(self.customCycleScrollView.mas_bottom).with.offset(20);
        make.height.mas_equalTo(40);
    }];
}

#pragma mark - setter & getter

- (UIImageView *)bgImageView
{
    if (!_bgImageView)
    {
        self.bgImageView = [[UIImageView alloc] init];
        self.bgImageView.image = [UIImage imageNamed:@"005.jpg"];
    }
    return _bgImageView;
}

- (UIScrollView *)bgScrollView
{
    if (!_bgScrollView)
    {
        self.bgScrollView = [[UIScrollView alloc] init];
        self.bgScrollView.backgroundColor = [UIColor clearColor];
        
        self.bgScrollView.contentSize = CGSizeMake(kWidth, 1200);
    }
    return _bgScrollView;
}

#pragma mark 輪播
- (SDCycleScrollView *)localCycleScrollView
{
    if (!_localCycleScrollView)
    {
        NSArray * localImageArray = @[@"h1.jpg",
                                      @"h2.jpg",
                                      @"h3.jpg",
                                      @"h4.jpg",
                                      @"h7" // 本地圖片請填寫全名
                                      ];
        
        self.localCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero shouldInfiniteLoop:YES imageNamesGroup:localImageArray];
        self.localCycleScrollView.delegate = self;
        self.localCycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.localCycleScrollView.autoScrollTimeInterval = 1;
    }
    return _localCycleScrollView;
}

- (SDCycleScrollView *)webCycleScrollView
{
    if (!_webCycleScrollView)
    {
        self.webCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        // 分頁控件的位置
        self.webCycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        
        // 標題
        self.webCycleScrollView.titlesGroup = @[@"1",@"2",@"3"];
        
        
        // 網絡圖片數組
        NSArray * imagesURLStrings = @[@"1",@"2",@"http://pic1.win4000.com/wallpaper/4/510f446941311.jpg"];
        self.webCycleScrollView.imageURLStringsGroup = imagesURLStrings;

    }
    return _webCycleScrollView;
}

- (SDCycleScrollView *)customCycleScrollView
{
    if (!_customCycleScrollView)
    {
        self.customCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        self.customCycleScrollView.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"];
        self.customCycleScrollView.pageDotImage = [UIImage imageNamed:@"pageControlDot"];
        
        // 網絡圖片數組
        NSArray *imagesURLStrings = @[
                                      @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",
                                      @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",
                                      @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"
                                      ];
        self.customCycleScrollView.imageURLStringsGroup = imagesURLStrings;
    }
    return _customCycleScrollView;
}

- (SDCycleScrollView *)textCycleScrollView
{
    if (!_textCycleScrollView)
    {
        self.textCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];
        
        self.textCycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.textCycleScrollView.onlyDisplayText = YES;

        self.textCycleScrollView.titlesGroup = @[@"第一個",@"第二個",@"第三個"];
    }
    return _textCycleScrollView;
}

@end

 

 設置默認展現第幾個app

1. SDCycleScrollView.h文件裏面添加一個初始化屬性ide

     @property(nonatomic,assign)NSUInteger firstIndex;//滾動的起始頁oop

 2. SDCycleScrollView.m裏面的layoutSubviews方法添加字體

  if(self.firstIndex !=0){動畫

     [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.firstIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];atom

     }

相關文章
相關標籤/搜索