iCarousel的简单介绍及应用

iOS开源类iCarousel介绍

iCarousel是一个类,它继承于UIView,用于简化实现各种类型的旋转木马(分页滚动视图)iPhone、iPad和Mac OS。iCarousel实现一些常见的影响如圆柱、平面式的旋转木马。经过 iCarousel类的封装,使iCarousel类的使用方式类似于UITableView的使用,每一个界面类似于一个单元格。 iCarousel内先创建一个可变字典,用于存储需要显示的单元格视图。创建一个父视图用于显示单元格视图,从字典中取出需要显示的单元格视图添加到创建父视图上,用于显示需要创建的单元格视图,在iCarousel类的内部对这些需要显示的单元格视图进行布局。

CarouselType,即Carousel类型

typedef NS_ENUM(NSUInteger, iCarouselType)
{
    iCarouselTypeLinear = 0,           //线性类型
    iCarouselTypeRotary,               //可旋转类型
    iCarouselTypeInvertedRotary,       //反向旋转类型
    iCarouselTypeCylinder,             //圆柱类型
    iCarouselTypeInvertedCylinder,     //反向圆柱类型
    iCarouselTypeWheel,                //车轮类型
    iCarouselTypeInvertedWheel,        //反向车轮类型
    iCarouselTypeCoverFlow,            //封面流类型
    iCarouselTypeCoverFlow2,           //封面流类型2
    iCarouselTypeTimeMachine,          //时光机类型
    iCarouselTypeInvertedTimeMachine,  //反向时光机类型
    iCarouselTypeCustom                //可自定义Carousel类型
};

常用属性介绍

//type是carousel展示样式,上面有介绍。
@property (nonatomic, assign) iCarouselType type;

//当用户用手指轻击carousel时,carousel的滚动速度,默认为1.0。
@property (nonatomic, assign) CGFloat scrollSpeed;

//能否滚动carousel,如果这个值被设为NO,carousel仍然可以以编程方式被滚动。
@property (nonatomic, assign, getter = isScrollEnabled) BOOL scrollEnabled;

//整页平移是否开启,一般设置为YES。
@property (nonatomic, assign, getter = isPagingEnabled) BOOL pagingEnabled;

//设置carousel在超出底部和返回时是否应该弹跳,或者是停止并挂掉。注意,在carousel样式设置为缠绕样式时或者carouselShouldWrap代理方法返回为yes时,这个属性不起作用。
@property (nonatomic, assign) BOOL bounces;


//这是以itemWidth的整数倍来计算的carousel当前的滚动偏移量,这个值,被截取为最接近的整数,是currentItemIndex值。当carousel运动中,你可以使用这个值定位其他屏幕的元素。这个值也可以被编程方式设置如果你想滚动carousel到一个特定的偏移。如果你想禁用内置手势处理并提供自己的实现时,这个可能有用。
@property (nonatomic, assign) CGFloat scrollOffset;

//这是当用户用手指拖动carousel时偏移量的乘数。它并不影响编程的滚动和减速的速度。对大多数carousel样式这个默认值是1.0,但是对CoverFlow-style样式的carousels默认值是2.0,来弥补他们的items在空间上更紧凑,所以必须拖拽更远来移动相同的距离的事实。你不能直接设置这个值,但是可以通过实现carouselOffsetMultiplier:代理方法来重写默认值。
@property (nonatomic, readonly) CGFloat offsetMultiplier;

//carousel中 items的数量(只读),要设置他的话,实现 numberOfItemsInCarousel:这个数据源方法。
@property (nonatomic, readonly) NSInteger numberOfItems;

//在carousel中展示的占位视图的数量(只读)。要设置他,实现一下numberOfPlaceholdersInCarousel:这个数据源方法。
@property (nonatomic, readonly) NSInteger numberOfPlaceholders;

//carousel中展示的items的宽度(只读)。这是自动从使用carousel:viewForItemAtIndex:reusingView:数据源方法第一个传到carousel中的视图中继承来的。你也可以使用carouselItemWidth:代理方法重写这个值,这个方法会改变分配给carousel items的空间(但是不会对这些item views重写设置大小或规模)。
@property (nonatomic, readonly) CGFloat itemWidth;

//当设置为YES时,点击任何在carousel 中的item而不是那个匹配currentItemIndex 的视图,将会使平滑动画移动到居中位置。点击当前被选中的item将没有效果。默认值是YES。
@property (nonatomic, assign) BOOL centerItemWhenSelected;


//默认情况下,不管carousel何时停止移动,他会自动滚动到最近的item 边界。如果你设置这个属性为NO,carousel停止后将不会滚动且不管在哪儿他都会停下来,即使他不是正好对准当前的索引。有一个特例,如果打包效果被禁止且bounces被设置为YES,然后,不管这个设置是什么,carousel会自动滚回第一个或者最后一个索引,如果它停下来时超出了carousel的底部。
@property (nonatomic, assign) BOOL scrollToItemBoundary;

iCarousel方法介绍

//顾名思义,这个方法是使carousel滚动到一个特定的item。
- (void)scrollToItemAtIndex:(NSInteger)indexanimated:(BOOL)animated;

//这个方法允许你来控制carousel使用 多长时间来滚动到特定的索引
- (void)scrollToItemAtIndex:(NSInteger)index duration:(NSTimeInterval)duration;


//这个方法工作起来和 scrollToItemAtIndex:方法一样,但是允许你移动到一个微小的偏移。如果你想达到一个非常准确的动画效果时这个可能有用。注意,如果scrollToItemBoundary属性被设置为YES,当你调用这个方法之后carousel会自动滚动到最近的item索引。
- (void)scrollToOffset:(CGFloat)offsetduration:(NSTimeInterval)duration;


//这个方法允许你使用一个固定的距离滚动carousel,以carousel的item宽度来衡量。整数或负数可能由itemCount来具体确定,取决于你希望滚动的方向。iCarousel很好的处理了边界问题,所以如果你指定了一个大于carousel中items数量的值,滚动或者在到达carousel底部时被夹紧(如果打包被禁止),或者无停顿地包裹。
- (void)scrollByNumberOfItems:(NSInteger)itemCount duration:(NSTimeInterval)duration;


//返回指定索引的item视图。
- (UIView *)itemViewAtIndex:(NSInteger)index;

//返回carousel视图的索引
- (NSInteger)indexOfItemView:(UIView *)view;

- (void)removeItemAtIndex:(NSInteger)index animated:(BOOL)animated;//移除视图
- (void)insertItemAtIndex:(NSInteger)index animated:(BOOL)animated;//插入视图
- (void)reloadItemAtIndex:(NSInteger)index animated:(BOOL)animated;//刷新视图

iCarouselDataSource

//返回carousel中界面的数量
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel;

//返回一个在carousel中要在指定索引处显示的视图,可以是UIImageView等,也可以自定义UIView
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view;


//返回在carousel中展示的占位视图数量。占位视图用来当carousel中界面太少而不能填满carousel的宽度,并且你希望在空白的地方显示一些东西时使用。它们随着carousel移动并且像其他carousel界面一样运行,但是它们不占numberOfItems数量,且不能被设置为当前选中的界面。
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel


//返回在carousel中展示的占位视图。
- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index reusingView:(UIView *)view;

iCarouselDelegate

//carousel将开始滚动动画
- (void)carouselWillBeginScrollingAnimation:(iCarousel *)carousel;
//carousel结束滚动动画
- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel;
//carousel滚动
- (void)carouselDidScroll:(iCarousel *)carousel;
//carousel变化
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel;
//开始拖动carousel
- (void)carouselWillBeginDragging:(iCarousel *)carousel;
//停止拖动carousel
- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate;
//carousel开始减速
- (void)carouselWillBeginDecelerating:(iCarousel *)carousel;
//carousel完成减速
- (void)carouselDidEndDecelerating:(iCarousel *)carousel;

//将要点击carousel视图触发该方法
- (BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index;
//点击carousel视图触发该方法,类似于tableView:didSelectRowAtIndexPath:方法
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index;

- (CGFloat)carouselItemWidth:(iCarousel *)carousel;
//自定义carousel动画
- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform;
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value;

iCarousel的自定义动画实现

创建iCarousel类的对象


- (iCarousel *)myCarousel {
    CGFloat height = kScreen_Width - 2*PAGE_OFFSET;
    if (!_myCarousel) {
        _myCarousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 100, kScreen_Width, height)];
        _myCarousel.dataSource = self;
        _myCarousel.delegate = self;
        _myCarousel.bounces = NO;
        _myCarousel.pagingEnabled = YES;
        _myCarousel.type = iCarouselTypeCustom;
    }
    return _myCarousel;
}

数据源

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] init];
        [_dataSource addObject:[NSString stringWithFormat:@"style_%@.jpg",@"1"]];
        [_dataSource addObject:[NSString stringWithFormat:@"style_%@.jpg",@"2"]];
        [_dataSource addObject:[NSString stringWithFormat:@"style_%@.jpg",@"3"]];
        [_dataSource addObject:[NSString stringWithFormat:@"style_%@.jpg",@"4"]];
        [_dataSource addObject:[NSString stringWithFormat:@"style_%@.jpg",@"5"]];
        [_dataSource addObject:[NSString stringWithFormat:@"style_%@.jpg",@"6"]];
        [_dataSource addObject:[NSString stringWithFormat:@"style_%@.jpg",@"7"]];
        [_dataSource addObject:[NSString stringWithFormat:@"style_%@.jpg",@"8"]];
    }
    return _dataSource;
}

实现代理函数

#pragma mark - iCarouselDataSource

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return self.dataSource.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
    if (view == nil) {
        CGFloat viewWidth = kScreen_Width - 2*PAGE_OFFSET;
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewWidth)];
    }
    ((UIImageView *)view).image = [UIImage imageNamed:[self.dataSource objectAtIndex:index]];
    
    return view;
}

#pragma mark - iCarouselDelegate

- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform {
    
    static CGFloat max_sacle = 1.0f;
    static CGFloat min_scale = 0.6f;
    if (offset <= 1 && offset >= -1) {
        float tempScale = offset < 0 ? 1+offset : 1-offset;
        float slope = (max_sacle - min_scale) / 1;
        
        CGFloat scale = min_scale + slope*tempScale;
        transform = CATransform3DScale(transform, scale, scale, 1);
    }else{
        transform = CATransform3DScale(transform, min_scale, min_scale, 1);
    }
    
    return CATransform3DTranslate(transform, offset * self.myCarousel.itemWidth * 1.4, 0.0, 0.0);
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
    [self showHudTipStr:[NSString stringWithFormat:@"点击了第%ld张图片",(long)index]];
}

总结

iCarousel是一个非常实用的第三方类,值得去更深入的学习它的用法!学习使人进步,既然选择了编程,就要坚持,爬着也要走下去。

 

备注:

autoscroll:

此属性控制着轮播器是否轮播轮播方向,以及轮播速度

当“autoscroll = 0”的时候,轮播器是不做轮播的。

只有在“autoscroll != 0”的时候轮播器在做轮播。

当“autoscroll < 0”的时候,轮播方向是从右向左滚动。

当“autoscroll > 0”的时候,轮播方向是从左往右滚动。

同时autoscroll的绝对值跟轮播的速度有关,越大滚动得越快越小滚动得越慢

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel;

iCarousel的DataSource方法,但是我在用的时候发现在type = iCarouselTypeInvertedCylinder的时候(我在开发的时候用的是此效果,其它效果未做测试)。这个属性并不向UITableView的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;这个方法那样好用。当我设置为_dataAry(数据源数组)的count时,中间有几个元素却未展示出来

但是如果此时return _dataAry.count * 2的话,那么就会把所有的都展示出来。
 

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIView *)view;

iCarousel的DataSource方法,在上文(return _dataAry.count * 2)情况下,在此方法里要注意数组别越界(_dataAry[index%_dataAry.count])。

另外可以先把要return 的view绘制好,并放入一个数组(_views)里面,在此方法里直接return。这样就不单单是图片轮播了,可以把需要展示的放到view里面展示出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值