使用iCarousel的旋转木马效果请求图片
生活随笔
收集整理的這篇文章主要介紹了
使用iCarousel的旋转木马效果请求图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用iCarousel的旋轉木馬效果請求圖片
https://github.com/nicklockwood/iCarousel
先看看效果:
源碼如下:
// // RootViewController.m // // Created by YouXianMing on 14-5-16. // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import "iCarousel.h" #import "YXJSON.h" #import "YXGCD.h" #import "SDWebImage.h"// 數據源 #define SOURCE_DATA @"http://www.duitang.com/album/1733789/masn/p/0/50/"@interface RootViewController ()<iCarouselDataSource, iCarouselDelegate>@property (nonatomic, strong) iCarousel *carousel; // iCarousel @property (nonatomic, strong) NSMutableArray *dataArray; // 數據源@end@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];// 初始化數據源_dataArray = [[NSMutableArray alloc] init];// 初始化iCarouselself.carousel = [[iCarousel alloc] initWithFrame:self.view.bounds];[self.view addSubview:_carousel];_carousel.backgroundColor = [UIColor blackColor];_carousel.type = iCarouselTypeWheel;// 設置代理self.carousel.delegate = self;self.carousel.dataSource = self;// 異步加載數據[[GCDQueue globalQueue] execute:^{// 獲取json數據NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:SOURCE_DATA]];// 轉換為字典NSDictionary *dataDic = [YXJSON dictionaryOrArrayWithJSONSData:data];if (dataDic){NSArray *dataArray = dataDic[@"data"][@"blogs"];for (NSDictionary *dic in dataArray){NSLog(@"%@", dic[@"isrc"]);// 存儲數據[_dataArray addObject:dic[@"isrc"]];}}// 主線程更新[[GCDQueue mainQueue] execute:^{// 重新加載carousel [_carousel reloadData];}];}]; }#pragma mark - #pragma mark iCarousel methods - (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {// 元素個數return [_dataArray count]; }- (UIView *)carousel:(iCarousel *)carouselviewForItemAtIndex:(NSUInteger)index // view的標志reusingView:(UIView *)view // 重用的view {if (view == nil){view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300.0f, 400.0f)];}// 強行轉換指針UIImageView *pointView = (UIImageView *)view;// 使用SDWebImage異步下載圖片 [pointView setImageWithURL:[NSURL URLWithString:_dataArray[index]]];// 圖片自動適應pointView.contentMode = UIViewContentModeScaleAspectFit;return view; }- (CGFloat)carousel:(iCarousel *)carouselvalueForOption:(iCarouselOption)optionwithDefault:(CGFloat)value {if (option == iCarouselOptionSpacing){return value * 1.1f;}return value; }@end RootViewController.m以下幾個地方使用了本人自己封裝的類,不開源,看官請自行替換相關方法-_-!
核心的地方如下:
so easy :)
?
?
問:如何實現view的點擊事件?
實現協議方法 - (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index; 即可
?
問:如何獲取偏移量?
實現協議,然后如下使用
- (void)carouselDidScroll:(iCarousel *)carousel
{
??? NSLog(@"scrollOffset %f", carousel.scrollOffset);
}
?
?
轉載于:https://www.cnblogs.com/YouXianMing/p/3732577.html
總結
以上是生活随笔為你收集整理的使用iCarousel的旋转木马效果请求图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 兼容Mono的下一代云环境Web开发框架
- 下一篇: ajax检查用户名