簡單實(shí)現(xiàn)下拉圖片放大③ - 定時(shí)器輪播圖

QQ20160908-2.png
  • 因?yàn)轫?xiàng)目還在更改,需求還沒定下來所以有時(shí)間做點(diǎn)小小的研究 ..

github下載地址點(diǎn)我

  • 言歸正傳看下效果 :
Untitled3.gif

細(xì)節(jié)包括 :

  • 拖動(dòng)停止定時(shí)器
  • 全屏拖動(dòng)pop返回
  • 無限輪播 無卡頓
  • 上滑漸變圖片消失
  • 上滑漸變出現(xiàn)導(dǎo)航欄 (自定義導(dǎo)航欄)
  • 狀態(tài)欄隨圖片漸變的顏色改變
  • 下拉放大
  • 層級機(jī)構(gòu)圖


    QQ20160907-0.png
  • viewController.m獲取數(shù)據(jù)
//創(chuàng)建圖片地址字符串?dāng)?shù)組即可! 檢查自己的是否支持HTTPS網(wǎng)絡(luò)請求
- (void)loadDataFromNet {
    _urls = @[@"http://imgsrc.baidu.com/baike/pic/item/a6efce1b9d16fdfa241bf189b68f8c5494ee7b65.jpg",
              @"http://pic2016.5442.com:82/2016/0811/26/1.jpg%21960.jpg",
              @"http://imgsrc.baidu.com/forum/pic/item/5bb5c9ea15ce36d3da6bfdb93af33a87e850b1cf.jpg",
              @"http://image.tianjimedia.com/uploadImages/2012/178/K830IZAG0Q20.jpg"];
}

注意:地址是百度的高清圖地址 .. 可能比較大

.

  • collectionView (輪播圖).m文件
    初始化接收數(shù)據(jù),并自定義flowlayout
- (instancetype)initWithUrls:(NSArray <NSURL *> *)urls
{
    self = [super initWithFrame:CGRectZero collectionViewLayout:[[YSFlowLayout alloc] init]];
    if (self) {
        _urls = urls;
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[YSCollectionViewCell class] forCellWithReuseIdentifier:YSCollectionViewCellId];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSIndexPath * path = [NSIndexPath indexPathForItem:_urls.count * 10 inSection:0];
            [self scrollToItemAtIndexPath:path atScrollPosition:0 animated:NO];
        });
        [self addTimer];

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tingzhi) name:@"tingzhi" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(jixu) name:@"jixu" object:nil];
    }
    return self;
}
  • flowlayout(布局item)
    .m文件
- (void)prepareLayout {
//    NSLog(@"%@",self.collectionView);
    self.minimumLineSpacing = 0;
    self.minimumInteritemSpacing = 0;
    self.itemSize = self.collectionView.bounds.size;
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView.showsVerticalScrollIndicator = 0;
    self.collectionView.showsHorizontalScrollIndicator = 0;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bigBig:) name:@"zys" object:nil];
}

注意點(diǎn)是 layout中有collectionView屬性,并且是有大小的 !

  • collectionViewCell.m
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_imageView];
    }
    return self;
}
  • collecView中的定時(shí)器
- (void)addTimer {
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)nextPage {
    NSUInteger page = _scrollV.contentOffset.x / self.hm_width + 1;
    NSLog(@"%zd",page);
    NSIndexPath * path = [NSIndexPath indexPathForItem:page  inSection:0];
    [self scrollToItemAtIndexPath:path atScrollPosition:0 animated:YES];
    
}



  • 內(nèi)容 :

    • 輪播圖拖動(dòng)定時(shí)器停止?jié)L動(dòng)
    • 停止拖動(dòng)添加定時(shí)器繼續(xù)滾動(dòng)
// zhu yi  yi chu  tong  zhi ..  !移!除!
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"tingzhi" object:nil userInfo:nil];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"jixu" object:nil userInfo:nil];
}
接收通知做調(diào)整.png
//移除定時(shí)器
- (void)tingzhi {
    [_timer invalidate];
    _timer = nil;
}
//添加定時(shí)器繼續(xù)
- (void)jixu {
    [self addTimer];
}
  • 原理:在scrollViewWillBeginDraggingscrollViewDidEndDragging代理方法里加通知中心
  • 注冊通知并接收通知即可

任何其他問題,歡迎留言,愿與你一起學(xué)習(xí)
郵箱:zh_yes@foxmail.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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