使用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检查用户名