iOS 实现立体轮播图(一行代码搞定)

github下载地址: https://github.com/zhangjiahuan8888/HHBannerView

示例图片

bannergif.gif

使用方法

1. 初始化

  • (instancetype)initWithFrame:(CGRect)frame WithBannerSource:(NinaBannerSource)bannerSource WithBannerArray:(NSArray *)bannerArray;

     

2. 设置属性

自动轮播间隔时间

@property (nonatomic, assign) CGFloat timeInterval;

是否要显示pagecontrol

@property (nonatomic, assign) BOOL showPageControl;

当前页点的颜色

@property (nonatomic, strong) UIColor *currentPageIndicatorTintColor;

未选中点的颜色

@property (nonatomic, strong) UIColor *pageIndicatorTintColor;

点击图片回调
  • (void)hhBannerView:(HHBannerView *)bannerView didSelectItemAtIndex:(NSInteger)index;

实现原理

简介

该控件主要使用iOS核心动画 CoreAnimation中转场动画-立方体翻转效果进行实现

 CATransition *transition = [[CATransition alloc] init];
transition.type = @"cube";                //立方体翻转
transition.subtype = kCATransitionFromRight;
transition.duration = 1.5;
transition.delegate = self;
[self.imgView.layer addAnimation:transition forKey:nil];
添加定时器进行自动的翻转
- (void)setupTimer:(CGFloat)timeInterval {
self.myTimer = [NSTimer timerWithTimeInterval:timeInterval target:self selector:@selector(handleSwipeFromRight) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.myTimer forMode:NSDefaultRunLoopMode];
}
添加左右滑动手势进行翻转动画

监听动画执行开始和结束,在开始时候关闭定时器,并且关闭self.img的触摸事件;在结束时候重新开启定时器,并且打开self.img的触摸事件

UISwipeGestureRecognizer *fromRightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight)];
[fromRightRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.imgView addGestureRecognizer:fromRightRecognizer];
UISwipeGestureRecognizer *fromLeftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft)];
[fromLeftRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.imgView addGestureRecognizer:fromLeftRecognizer];

更多源码请访问github:https://github.com/zhangjiahuan8888

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!
技术狗 » iOS 实现立体轮播图(一行代码搞定)