星空网 > 软件开发 > 操作系统

RoateView

 1 #import <UIKit/UIKit.h> 2  3 typedef void(^Click)(NSInteger index); 4  5 @interface SLRoateView : UIView 6 /* 7  // tableView 中cell的复用机制始终没整出来 所以现在只能放image 放tableview没有意义 因为怎么都要创建数组这么多个tableview 8 { 9 // 当scrollView装载的别的View 而不是imageView 比如一些App中scrollVeiw上放的的tableView 等等用以分类展示的view 像爱奇艺首页的电影、电视、综艺、动漫 这样就不允许有轮播和点击属性10 11 // 是否允许轮播 默认允许 NO12 @property(nonatomic, assign)BOOL AllowCarousel;13 14 // 是否支持点击 只需判断block 是否为空 为空则不添加手势15 @property(nonatomic, assign)BOOL SupportInteraction;16 }17 */18 // 创建19 + (SLRoateView *)roateViewWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click;20 21 //- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click;22 23 @end

#import "SLRoateView.h"@interface SLRoateView ()<UIScrollViewDelegate>@property(nonatomic, strong)NSArray *array;@property(nonatomic, copy)Click click;@property(nonatomic, assign)CGFloat KRoateWidth;@property(nonatomic, assign)CGFloat KRoateHeight;@property(nonatomic, strong)UIImageView *leftImageView;@property(nonatomic, strong)UIImageView *centerImageView;@property(nonatomic, strong)UIImageView *rightImageView;@property(nonatomic, assign)NSInteger index;@property(nonatomic, strong)UIPageControl *pageControl;@property(nonatomic, strong)NSTimer *timer;@property(nonatomic, strong)UIScrollView *scroll;@end@implementation SLRoateView+ (SLRoateView *)roateViewWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click{  SLRoateView *roateView = [[SLRoateView alloc]initWithFrame:frame array:array click:click];  return roateView;}- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array click:(Click)click{  self = [super initWithFrame:frame];  if (self) {    self.KRoateWidth = frame.size.width;    self.KRoateHeight = frame.size.height;    self.array = array;    self.click = click;    // scrollVeiw 设置    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, _KRoateWidth, _KRoateHeight)];    self.scroll.contentSize = CGSizeMake(_KRoateWidth * 3, 0);    self.scroll.pagingEnabled = YES;    self.scroll.contentOffset = CGPointMake(_KRoateWidth, 0);    self.scroll.alwaysBounceVertical = NO;    self.scroll.showsVerticalScrollIndicator = NO;    self.scroll.alwaysBounceHorizontal = YES;    self.scroll.showsHorizontalScrollIndicator = NO;    self.scroll.delegate = self;    [self addSubview:_scroll];        [self addGestureRecognizer];    [self addImageViews];    [self setDefautImage];    [self addPageControl];        [self addTimer];  }  return self;}#pragma mark 添加计时器- (void)addTimer{  // 在子线程中创建一个计时器 是图片实现轮播  dispatch_async(dispatch_get_global_queue(0, 0), ^{    _timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(rotaView) userInfo:nil repeats:YES];    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];        [[NSRunLoop currentRunLoop] run];      });}- (void)rotaView{  [_scroll setContentOffset:CGPointMake(_KRoateWidth * 2, 0) animated:YES];  NSLog(@"%ld", _index);  // 设置分页 由于动画是从中间移动到第三张 所以pageControl要显示的不是中间图片而是第三张图片  _pageControl.currentPage = (_index + 1) % _array.count;  if (_scroll.contentOffset.x/_KRoateWidth == 2)  {    [self reloadImage];    // scrollView 回到中间    [self.scroll setContentOffset:CGPointMake(_KRoateWidth, 0) animated:NO];  }}#pragma mark 添加点击事件- (void)addGestureRecognizer{  [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureRecognizer)]];}- (void)tapGestureRecognizer{  if (_click)  {    // 用当前的    self.click(_pageControl.currentPage);  }  }#pragma mark 添加图片控件- (void)addImageViews{  _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _KRoateWidth, _KRoateHeight)];  // imageView 大小不会改变 UIViewContentModeScaleAspectFit 只是image大小变化了//  _leftImageView.contentMode = UIViewContentModeScaleAspectFit;  [self.scroll addSubview:_leftImageView];    _centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_KRoateWidth, 0, _KRoateWidth, _KRoateHeight)];//  _centerImageView.contentMode = UIViewContentModeScaleAspectFit;  [self.scroll addSubview:_centerImageView];  _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_KRoateWidth * 2, 0, _KRoateWidth, _KRoateHeight)];//  _rightImageView.contentMode = UIViewContentModeScaleAspectFit;  [self.scroll addSubview:_rightImageView];  NSLog(@" %f %f %f", _leftImageView.frame.size.height, _centerImageView.frame.size.height, _rightImageView.frame.size.height);}#pragma mark 设置默认显示图片- (void)setDefautImage{  _leftImageView.image = _array[_array.count - 1];  _centerImageView.image = _array[0];  _rightImageView.image = _array[1];  _index = 0;  _pageControl.currentPage = _index;}#pragma mark 添加分页控件- (void)addPageControl{    // 根据页数放回UIPageControl合适的大小 返回(0,0)不知道为啥 ----先要创建出来再给frame  _pageControl = [[UIPageControl alloc]init];  CGSize size = [_pageControl sizeForNumberOfPages:_array.count];  _pageControl.frame = CGRectMake(0, 0, size.width, size.height);  _pageControl.center = CGPointMake(_KRoateWidth / 2, _KRoateHeight - size.height/2);  _pageControl.numberOfPages = _array.count;  [self addSubview:_pageControl];}//#pragma mark 拖拽事件//- (void)scrollViewDidScroll:(UIScrollView *)scrollView//{//  if (scrollView.isDragging)//  {//    NSLog(@"关闭定时器");//    [_timer setFireDate:[NSDate distantFuture]];//  }//  if (!scrollView.isDragging)//  {//    NSLog(@"asdfasdf");//  }//}#pragma mark 滚动停止事件- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{  // 5秒后启动计时器  [_timer performSelector:@selector(setFireDate:) withObject:[NSDate distantPast] afterDelay:5];  [self reloadImage];  // scrollView 回到中间  [self.scroll setContentOffset:CGPointMake(_KRoateWidth, 0) animated:NO];    // 设置分页  _pageControl.currentPage = _index;  }#pragma mark 重新加载图片- (void)reloadImage{  NSInteger leftIndex, rigtIndex;  CGPoint offset = self.scroll.contentOffset;  if (offset.x > _KRoateWidth)  {    _index = (_index + 1) % _array.count;  }else if (offset.x < _KRoateWidth)  {    _index = (_index + _array.count - 1) % _array.count;  }  _centerImageView.image = _array[_index];    leftIndex = (_index + _array.count - 1) % _array.count;  rigtIndex = (_index + 1) % _array.count;  _leftImageView.image = _array[leftIndex];  _rightImageView.image = _array[rigtIndex];}- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{  if (scrollView.isDragging)  {    // 取消timer之前的执行请求    [NSObject cancelPreviousPerformRequestsWithTarget:_timer];    // 关闭定时器    [_timer setFireDate:[NSDate distantFuture]];      }}

 




原标题:RoateView

关键词:ie

ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

资金补贴:https://www.goluckyvip.com/tag/49667.html
资深跨境电商观察者:https://www.goluckyvip.com/tag/49668.html
资讯侦察阿sir:https://www.goluckyvip.com/tag/49669.html
订单取消率:https://www.goluckyvip.com/tag/4967.html
淄博市热力集团:https://www.goluckyvip.com/tag/49670.html
子秋茗马:https://www.goluckyvip.com/tag/49671.html
石象湖景区门票-石象湖景区门票优惠政策:https://www.vstour.cn/a/411243.html
北京到嵩山自驾游沿途景点 北京距离嵩山有多远:https://www.vstour.cn/a/411244.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流