iCarousel 示例(iCarousel 自带样式)

来源:互联网 发布:java 时区时间转换 编辑:程序博客网 时间:2024/05/31 19:34

关于 iCarousel 自带效果示例

#import <iCarousel.h>#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.heigh@interface ViewController ()<iCarouselDataSource,iCarouselDelegate>@property (nonatomic, strong) iCarousel *carouselView;@property (nonatomic, strong) NSMutableArray *arrItems;@property (nonatomic, strong) UIBarButtonItem *btnSelectType;@property (nonatomic, assign) iCarouselType carouselType;@end

viewDidLoad 中实现

self.title = @"iCarouselTypeLinear";  self.view.backgroundColor = [UIColor blackColor];[self.view addSubview:self.carouselView];self.navigationItem.rightBarButtonItem = self.btnSelectType;

返回 item 数量

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{    return self.arrItems.count;}

返回 item 上视图

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(nullable UIView *)view{    UILabel *label = nil;    //create new view if no view is available for recycling    if (view == nil)    {        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];        ((UIImageView *)view).image = [UIImage imageNamed:@"page.png"];        view.contentMode = UIViewContentModeCenter;        label = [[UILabel alloc] initWithFrame:view.bounds];        label.backgroundColor = [UIColor clearColor];        label.textAlignment = NSTextAlignmentCenter;        label.font = [label.font fontWithSize:50];        label.tag = 1;        [view addSubview:label];    }    else    {        //get a reference to the label in the recycled view        label = (UILabel *)[view viewWithTag:1];    }    //set item label    //remember to always set any properties of your carousel item    //views outside of the `if (view == nil) {...}` check otherwise    //you'll get weird issues with carousel item content appearing    //in the wrong place in the carousel    label.text = [self.arrItems[index] stringValue];    return view;}

item 点击事件

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{    NSNumber *item = (self.arrItems)[index];    NSLog(@"Tapped view number: %@", item);}

可选项设置

- (CGFloat)carousel:(iCarousel *)_carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value{    //customize carousel display    switch (option)    {        case iCarouselOptionSpacing:        {            //add a bit of spacing between the item views            return value * 1.05f;        }        case iCarouselOptionFadeMax:        {            if (self.carouselView.type == iCarouselTypeCustom)            {                //set opacity based on distance from camera                return 0.0f;            }            return value;        }        default:        {            return value;        }    }}

占位图实现(可选)

- (NSInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel{    //note: placeholder views are only displayed on some carousels if wrapping is disabled    return 0;}- (UIView *)carousel:(iCarousel *)carousel placeholderViewAtIndex:(NSInteger)index reusingView:(UIView *)view{    UILabel *label = nil;    //create new view if no view is available for recycling    if (view == nil)    {        //don't do anything specific to the index within        //this `if (view == nil) {...}` statement because the view will be        //recycled and used with other index values later        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];        ((UIImageView *)view).image = [UIImage imageNamed:@"page.png"];        view.contentMode = UIViewContentModeCenter;        label = [[UILabel alloc] initWithFrame:view.bounds];        label.backgroundColor = [UIColor clearColor];        label.textAlignment = NSTextAlignmentCenter;        label.font = [label.font fontWithSize:50.0f];        label.tag = 1;        [view addSubview:label];    }    else    {        //get a reference to the label in the recycled view        label = (UILabel *)[view viewWithTag:1];    }    //set item label    //remember to always set any properties of your carousel item    //views outside of the `if (view == nil) {...}` check otherwise    //you'll get weird issues with carousel item content appearing    //in the wrong place in the carousel    label.text = (index == 0)? @"[": @"]";    return view;}

barButton 点击事件

- (void)barSeletType:(UIBarButtonItem *)barBtnSelectType{    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:(UIAlertControllerStyleActionSheet)];    UIAlertAction *actionLinear = [UIAlertAction actionWithTitle:@"" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeLinear;        self.title = @"iCarouselTypeLinear";        [self changeCarouselViewType];    }];    UIAlertAction *actionRotary = [UIAlertAction actionWithTitle:@"iCarouselTypeRotary" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeRotary;        self.title = @"iCarouselTypeRotary";        [self changeCarouselViewType];    }];    UIAlertAction *actionInvertedRotary = [UIAlertAction actionWithTitle:@"iCarouselTypeInvertedRotary" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeInvertedRotary;        self.title = @"iCarouselTypeInvertedRotary";        [self changeCarouselViewType];    }];    UIAlertAction *actionCylinder = [UIAlertAction actionWithTitle:@"iCarouselTypeCylinder" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeCylinder;        self.title = @"iCarouselTypeCylinder";        [self changeCarouselViewType];    }];    UIAlertAction *actionInvertedCylinder = [UIAlertAction actionWithTitle:@"iCarouselTypeInvertedCylinder" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeInvertedCylinder;        self.title = @"iCarouselTypeInvertedCylinder";        [self changeCarouselViewType];    }];    UIAlertAction *actionWheel = [UIAlertAction actionWithTitle:@"iCarouselTypeWheel" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeWheel;        self.title = @"iCarouselTypeWheel";        [self changeCarouselViewType];    }];    UIAlertAction *actionInvertedWheel = [UIAlertAction actionWithTitle:@"iCarouselTypeInvertedWheel" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeInvertedWheel;        self.title = @"iCarouselTypeInvertedWheel";        [self changeCarouselViewType];    }];    UIAlertAction *actionCoverFlow = [UIAlertAction actionWithTitle:@"iCarouselTypeCoverFlow" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeCoverFlow;        self.title = @"iCarouselTypeCoverFlow";        [self changeCarouselViewType];    }];    UIAlertAction *actionCoverFlow2 = [UIAlertAction actionWithTitle:@"iCarouselTypeCoverFlow2" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeCoverFlow2;        self.title = @"iCarouselTypeCoverFlow2";        [self changeCarouselViewType];    }];    UIAlertAction *actionTimeMachine = [UIAlertAction actionWithTitle:@"iCarouselTypeTimeMachine" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeTimeMachine;        self.title = @"iCarouselTypeTimeMachine";        [self changeCarouselViewType];    }];    UIAlertAction *actionInvertedTimeMachine = [UIAlertAction actionWithTitle:@"iCarouselTypeInvertedTimeMachine" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {        self.carouselType = iCarouselTypeInvertedTimeMachine;        self.title = @"iCarouselTypeInvertedTimeMachine";        [self changeCarouselViewType];    }];    [alertController addAction:actionLinear];    [alertController addAction:actionRotary];    [alertController addAction:actionInvertedRotary];    [alertController addAction:actionCylinder];    [alertController addAction:actionInvertedCylinder];    [alertController addAction:actionWheel];    [alertController addAction:actionInvertedWheel];    [alertController addAction:actionCoverFlow];    [alertController addAction:actionCoverFlow2];    [alertController addAction:actionTimeMachine];    [alertController addAction:actionInvertedTimeMachine];    [self.navigationController presentViewController:alertController animated:YES completion:nil];}- (void)changeCarouselViewType{    [UIView beginAnimations:nil context:nil];    self.carouselView.type = self.carouselType;    [UIView commitAnimations];}
//- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform//{//    //implement 'flip3D' style carousel//    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);//    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.carouselView.itemWidth);//}

懒加载

- (iCarousel *)carouselView{    if (!_carouselView)    {        _carouselView = [[iCarousel alloc] initWithFrame:self.view.frame];        _carouselView.delegate = self;        _carouselView.dataSource = self;        _carouselView.type = iCarouselTypeLinear;    }    return _carouselView;}- (NSMutableArray *)arrItems{    if (!_arrItems)    {        _arrItems = [[NSMutableArray alloc] init];        for (NSInteger i = 0; i < 15; i++)        {            [_arrItems addObject:@(i)];        }    }    return _arrItems;}- (UIBarButtonItem *)btnSelectType{    if (!_btnSelectType)    {        _btnSelectType = [[UIBarButtonItem alloc] initWithTitle:@"Select" style:(UIBarButtonItemStylePlain) target:self action:@selector(barSeletType:)];    }    return _btnSelectType;}

linear 效果:
linear

rotary 效果图:
rotary

invertedRotary 效果图:
invertedRotary

cylinder 效果图:
cylinder

invertedCylinder 效果图:
invertedCylinder

wheel 效果图:
wheel

invertedWheel 效果图:
invertedWheel

coverFlow 效果图:
coverFlow

coverFlow2 效果图:
coverFlow2

timeMachine 效果图:
timeMachine

invertedTimeMachine 效果图:
invertedTimeMachine

原创粉丝点击