iOS 旋转木马效果【优化版】

来源:互联网 发布:手机淘宝店铺海报图片 编辑:程序博客网 时间:2024/05/02 12:59

之前写了一个旋转木马效果,朋友们会时不时的找我要,今天索性在博客里面写一下这个iCarousel类怎么使用才能符合产品的需求。

首先让我们来了解iCarousel下面这位大神给我们带来的福音。


Created by Nick Lockwood on 01/04/2011.

Copyright 2010 Charcoal Design. All rights reserved.

代码比较老,最后更新于2011年,哈哈,和我iOS编程的年限一样了,老家伙还是需要老家伙玩转。偷笑

言归正传:go,看一个第三方类库最先看哪些内容呢?先看Delegate和DataSource 

@protocol iCarouselDataSource <NSObject>- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel;- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index;@optional- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel;- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSUInteger)index;- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel;@end@protocol iCarouselDelegate <NSObject>@optional-(void)hideArrowView;-(void)showArrowView;- (void)carouselWillBeginScrollingAnimation:(iCarousel *)carousel;- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousel;- (void)carouselDidScroll:(iCarousel *)carousel;- (void)carouselCurrentItemIndexUpdated:(iCarousel *)carousel;- (void)carouselWillBeginDragging:(iCarousel *)carousel;- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate;- (void)carouselWillBeginDecelerating:(iCarousel *)carousel;- (void)carouselDidEndDecelerating:(iCarousel *)carousel;- (CGFloat)carouselItemWidth:(iCarousel *)carousel;- (CGFloat)carouselOffsetMultiplier:(iCarousel *)carousel;- (BOOL)carouselShouldWrap:(iCarousel *)carousel;- (CATransform3D)carousel:(iCarousel *)carousel transformForItemView:(UIView *)view withOffset:(CGFloat)offset;#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED- (BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index;- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index;#endif

通过上面的Delegate和DataSource我们能够看到这个类支持哪些功能,英语不懂怎么办?直接回初中接受基础教育或者用蹩脚的google翻译。

那么有了这个第三方类了,怎么在项目中使用呢?

首先:导入  #import"iCarousel.h"
然后 设置一下 <iCarouselDelegate,iCarouselDataSource>

实现如下图的效果:
  我和你 

效果描述:旋转木马效果,通过旋转实现中间View永远是最大的,当移动是两边的View会随之变化。箭头在移动的时候也会消失,当静止时显示出来。
然后 在viewDidLoad中开始编码,

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    UIImageView *backGroudImg =[[UIImageView alloc]initWithFrame:self.view.bounds];    backGroudImg.image=[UIImage imageNamed:@"background"];    [self.view addSubview:backGroudImg];        _carousel=[[iCarousel alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, 300)];    _carousel.type=iCarouselTypeCoverFlow;    _carousel.delegate=self;    _carousel.dataSource=self;        [self.view addSubview:_carousel];        _ArrowLeftImgView =[[UIImageView alloc]initWithFrame:CGRectMake(45, 200, 22, 25)];        _ArrowLeftImgView.image=[UIImage imageNamed:@"arrowleft"];    [self.view addSubview:_ArrowLeftImgView];        _ArrowRightImgView =[[UIImageView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width-45-22, 200, 22, 25)];        _ArrowRightImgView.image=[UIImage imageNamed:@"arrowright"];    [self.view addSubview:_ArrowRightImgView];    }

实现Delegate和DataSource
#pragma mark iCarouselDataSource//有多少项- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel{    return 3;}//最大有多少个可以显示- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel{    return 21;}//每一个的内容- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index{//        NSMutableArray *titleNameArray =[[NSMutableArray alloc]init];    [titleNameArray addObject:@"shouji"];    [titleNameArray addObject:@"xuexin"];    [titleNameArray addObject:@"dianshang"];        [titleNameArray replaceObjectAtIndex:1 withObject:@"xuexin2"];    UIImageView *imgv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];    imgv.image=[UIImage imageNamed:[titleNameArray objectAtIndex:index]];    imgv.tag = index;            return imgv ;}#pragma mark -#pragma mark iCarouselDelegate-(CGFloat)carouselItemWidth:(iCarousel *)carousel{    return 350;}- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{    NSLog(@"---->%ld", (long)index);    switch (index) {        case 0:        case 1:        {        }            break;             default:            break;    }    }-(void)hideArrowView{        NSLog(@"$$$$$$$");    _ArrowLeftImgView.hidden=YES;    _ArrowRightImgView.hidden=YES;    }-(void)showArrowView{    NSLog(@"^^^^^^^^");   _ArrowLeftImgView.hidden=NO;    _ArrowRightImgView.hidden=NO;}

好了,这样我们就实现了产品需求的样式。

那么,那么 是不是感觉我不够帅,big不够高?

等等,还有内容要给你

look at 项目源码下载:














0 0
原创粉丝点击