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文章

iOS CALayer的使用详细教程

2019-12-30 21:57:30

iOS文章

IOS中一段文字设置多种字体颜色代码的方法

2019-12-30 22:48:01

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索