Use iCarousel to achieve the effect of 3D horizontal screen sliding

iCarousel introduction

Open source address: https://github.com/nicklockwood/iCarousel

iCarousel is a class based on scrollView. The main effect type of this class is rotation. For iphone, iPad and Mac OS. iCarousel already comes with many basic effects.

As a third-party framework integrated into the project, iCarousel has several advantages:
1. You only need to import iCarousel.h and iCarousel.m files to use.
2. The API interface of the framework is very convenient, so that the user can not modify the internal implementation too much.
3. In order to achieve more complex effects, the framework can also be easily solved by implementing the proxy and data source methods.

Use iCarousel to achieve the effect of 3D horizontal screen sliding

  • Statement iCarousel
@property (nonatomic, strong) iCarousel                     icarouselView;
  • Add iCarousel view
- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view addSubview:self.icarouselView];
    [self.icarouselView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(0);
    }];

}
  • Implementation of data source method
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)cell
{
    if (!cell)
    {
           cell = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    }

    cell.delegate = self;
    return cell;
}
  • Implementing agent methods
- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    static CGFloat max_sacle = 1.0f;
    static CGFloat min_scale = 0.88f;
    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 * 400 * 1.15, 0.0, 0.0);
}
  • Lazy loading of iCarousel
- (iCarousel*)icarouselView
{
    if(!_icarouselView)
    {
        _icarouselView = [[iCarousel alloc] init];
        _icarouselView.type = iCarouselTypeCustom;
        _icarouselView.delegate = self;
        _icarouselView.dataSource = self;
        _icarouselView.pagingEnabled = YES;
        _icarouselView.bounces = NO;
        _icarouselView.layer.cornerRadius = 2.f;
        _icarouselView.layer.masksToBounds = YES;
    }
    return _icarouselView;
}

Summary and suggestions:

I just used this class and simple custom functions in the development process. In fact, the type of this class has many effects. It is not difficult to study the internal implementation of this class in the spare time in the future.

Keywords: github Mac

Added by Jim_Bo on Wed, 15 Apr 2020 21:36:44 +0300