IOS 开发之网络图片轮播图的实现

 更新时间:2017年09月17日 15:57:52   作者:水桶前辈  
这篇文章主要介绍了IOS 开发之网络图片轮播图的实现的相关资料,希望通过此文大家能够掌握轮播图的实现,需要的朋友可以参考下

IOS 开发之网络图片轮播图的实现

截图

1.使用

LJPhotoGroupView *_ljPhotoGroupView = [[LJPhotoGroupView alloc]initWithItem:self.ljUrlArray]; 
_ljPhotoGroupView.backgroundColor = [UIColor blackColor]; 
_ljPhotoGroupView.frame = CGRectMake(0, 0, kDEVICEWIDTH, kDEVICEHEIGHT); 
[_ljPhotoGroupView showHintView:self]; 

2.源码

#import "LJPhotoGroupView.h" 
#import "LJWebIDataManager.h" 
 
@interface LJPhotoGroupCellView() 
 
@property (nonatomic, strong) UIImageView *ljImageview; 
 
@end 
 
@implementation LJPhotoGroupCellView 
 
- (instancetype)initWithFrame:(CGRect)frame url:(NSString*)imageurl 
{ 
  self = [super initWithFrame:frame]; 
  if (self) { 
    [self addSubview:self.ljImageview]; 
    //这里大家可以换成自己的网络请求图片的方法 
    [[LJWebIDataManager sharedInstances]retrieveData:imageurl successBlock:^(NSData *netData, NSString *progressStr, BOOL isFinished) { 
      //在主线程中刷新界面 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
 
        UIImage *_ljImage = [UIImage imageWithData: netData scale:0.3]; 
        @myWeakify(self); 
        dispatch_async(dispatch_get_main_queue(), ^{ 
          @myStrongify(self); 
          self.ljImageview.image = _ljImage; 
        }); 
      }); 
    }]; 
  } 
  return self; 
} 
 
- (UIImageView*)ljImageview 
{ 
  if (!_ljImageview) { 
    _ljImageview = UIImageView.new; 
    _ljImageview.frame = CGRectMake(15, 0, kDEVICEWIDTH - 30, 130); 
    _ljImageview.backgroundColor = [UIColor redColor]; 
    UIGestureRecognizer *_tap = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(dismissHintView)]; 
    [_ljImageview addGestureRecognizer:_tap]; 
  } 
  return _ljImageview; 
} 
 
@end 
 
@interface LJPhotoGroupView()<UIScrollViewDelegate> 
 
@property (nonatomic, strong) UIScrollView *ljScrollView; 
@property (nonatomic, strong) NSArray *ljItemArray; 
@property (nonatomic, strong) UIImageView *ljImageview; 
@property (nonatomic, strong) UIPageControl *ljPageControl; 
 
 
@end 
 
@implementation LJPhotoGroupView 
 
- (instancetype)initWithItem:(NSArray*)ljArray 
{ 
  self = [super init]; 
  if (self) 
  { 
    self.ljItemArray = [NSArray arrayWithArray:ljArray]; 
     
    [self addSubview:self.ljScrollView]; 
    [self addSubview:self.ljPageControl]; 
     
    for (int i = 0; i < self.ljItemArray.count; i++) { 
       
      //方法一:直接设置每个cell的X坐标 
//      LJPhotoGroupCellView *_cell = [[LJPhotoGroupCellView alloc]initWithFrame:CGRectMake((kDEVICEWIDTH )*i, 0, kDEVICEWIDTH, 130) url:self.ljItemArray[i]]; 
       
      //方法二:先不用考虑cell的X坐标,在下面设置X的坐标 
      LJPhotoGroupCellView *cell = [[LJPhotoGroupCellView alloc]initWithFrame:self.ljScrollView.bounds url:self.ljItemArray[i]]; 
      UITapGestureRecognizer *_tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissHintView)]; 
        [cell addGestureRecognizer:_tap]; 
      [self.ljScrollView addSubview:cell]; 
    } 
     
    //方法二:设置cell的X坐标 
    // 计算imageView的位置 
    [self.ljScrollView.subviews enumerateObjectsUsingBlock:^(LJPhotoGroupCellView *cell, NSUInteger idx, BOOL *stop) 
     { 
      // 调整x => origin => frame 
      CGRect frame = cell.frame; 
      frame.origin.x = idx * frame.size.width; 
       
      cell.frame = frame; 
    }]; 
     
    self.ljPageControl.currentPage = 0; 
  } 
  return self; 
} 
 
- (UIScrollView*)ljScrollView 
{ 
  if (!_ljScrollView) 
  { 
    _ljScrollView = UIScrollView.new; 
    _ljScrollView.frame = CGRectMake(0, 250, kDEVICEWIDTH, 130); 
    _ljScrollView.delegate = self; 
    //_scrollView.scrollsToTop = NO; 
    _ljScrollView.pagingEnabled = YES; 
    _ljScrollView.contentSize = CGSizeMake(_ljScrollView.bounds.size.width * self.ljItemArray.count, 130); 
    //_scrollView.alwaysBounceHorizontal = groupItems.count > 1; 
    // _scrollView.showsHorizontalScrollIndicator = NO; 
    //_scrollView.showsVerticalScrollIndicator = NO; 
    //_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    //_scrollView.delaysContentTouches = NO; 
    //_scrollView.canCancelContentTouches = YES; 
  } 
  return _ljScrollView; 
} 
 
- (UIPageControl *)ljPageControl 
{ 
  if (_ljPageControl == nil) 
  { 
    // 分页控件,本质上和scrollView没有任何关系,是两个独立的控件 
    _ljPageControl = [[UIPageControl alloc] init]; 
    // 总页数 
    _ljPageControl.numberOfPages = self.ljItemArray.count; 
    CGSize size = [_ljPageControl sizeForNumberOfPages:self.ljItemArray.count]; 
     
    _ljPageControl.bounds = CGRectMake(0, 0, size.width, size.height); 
    _ljPageControl.center = CGPointMake(self.center.x, 380); 
     
    // 设置颜色 
    _ljPageControl.pageIndicatorTintColor = [UIColor redColor]; 
    //当前页面的颜色 
    _ljPageControl.currentPageIndicatorTintColor = [UIColor whiteColor]; 
    [_ljPageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged]; 
  } 
  return _ljPageControl; 
} 
 
// 分页控件的监听方法 
- (void)pageChanged:(UIPageControl *)page 
{ 
  NSLog(@"%ld", (long)page.currentPage); 
   
  // 根据页数,调整滚动视图中的图片位置 contentOffset self.scrollView.bounds.size.width 
  CGFloat x = page.currentPage * (kDEVICEWIDTH); 
  [self.ljScrollView setContentOffset:CGPointMake(x, 0) animated:YES]; 
} 
 
- (UIImageView*)ljImageview 
{ 
  if (!_ljImageview) { 
    _ljImageview = UIImageView.new; 
    _ljImageview.frame = CGRectMake(0, 0, kDEVICEWIDTH, kDEVICEHEIGHT); 
    _ljImageview.backgroundColor = [UIColor redColor]; 
     
    UIGestureRecognizer *_tap = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(dismissHintView)]; 
    [_ljImageview addGestureRecognizer:_tap]; 
  } 
  return _ljImageview; 
} 
 
 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
  //CGFloat floatPage = _scrollView.contentOffset.x / _scrollView.width; 
  //NSInteger page = _scrollView.contentOffset.x / _scrollView.width; 
} 
 
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 
  if (!decelerate) { 
  } 
} 
 
 
#pragma mark - ScrollView的代理方法 
// 滚动视图停下来,修改页面控件的小点(页数) 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
  // 停下来的当前页数 
  NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset)); 
   
  // 计算页数 
  int page = scrollView.contentOffset.x / scrollView.bounds.size.width; 
   
  self.ljPageControl.currentPage = page; 
} 
 
 
- (void)showHintView:(UIView*)view 
{ 
  //[view addSubview:self]; 
   [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self]; 
   
  self.alpha = 0.0; 
  [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     
    self.alpha = 1.0; 
     
  } completion:^(BOOL finished) 
   { 
      
   }]; 
} 
 
- (void)dismissHintView 
{ 
  [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ 
     
    self.alpha = 0.0; 
     
  } completion:^(BOOL finished){ 
     
    [self removeFromSuperview]; 
  }]; 
} 
 
@end 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • iOS抽屉效果开发案例分享

    iOS抽屉效果开发案例分享

    这篇文章主要为大家分享了iOS抽屉效果开发案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • 深入理解iOS的状态栏

    深入理解iOS的状态栏

    这篇文章给大家分别介绍了iOS状态栏隐藏的两种方法、状态栏样式、背景色以及状态栏的应用,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-09-09
  • IOS关闭键盘的方法

    IOS关闭键盘的方法

    在iOS应用开发中,有三类视图对象会打开虚拟键盘,进行输入操作,但如何关闭虚拟键盘,却没有提供自动化的方法。这个需要我们自己去实现。
    2015-05-05
  • iOS如何获取手机的Mac地址

    iOS如何获取手机的Mac地址

    这篇文章主要为大家详细介绍了iOS获取手机的Mac地址的多种方法,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • iOS开发实现简单计算器功能

    iOS开发实现简单计算器功能

    这篇文章主要为大家详细介绍了iOS开发实现简单计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • iOS中UILabel设置居上对齐、居中对齐、居下对齐及文字置顶显示

    iOS中UILabel设置居上对齐、居中对齐、居下对齐及文字置顶显示

    这篇文章主要给大家介绍了关于iOS中UILabel如何设置居上对齐、居中对齐、居下对齐及文字置顶显示效果的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • iOS中遍历的方法总结

    iOS中遍历的方法总结

    本篇文章主要介绍了iOS中遍历的方法总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • iOS获取Label高度的几种方法与对比

    iOS获取Label高度的几种方法与对比

    这篇文章主要介绍了给大家介绍了iOS获取Label高度的几种方法,包括 view的sizeThatFits 方法、view的sizeToFit方法、NSString的sizeWithAttributes方法和NSString 的 boundingRectWithSize 方法,文中不仅介绍四种方法的实现,还进行了对比,下面来一起看看吧。
    2016-11-11
  • iOS轻点、触摸和手势代码开发

    iOS轻点、触摸和手势代码开发

    这篇文章主要为大家详细介绍了iOS轻点、触摸和手势代码开发,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • IOS UIView的生命周期的实例详解

    IOS UIView的生命周期的实例详解

    这篇文章主要介绍了IOS UIView的生命周期的实例详解的相关资料,希望通过本文大家能掌握理解这部分内容,需要的朋友可以参考下
    2017-09-09

最新评论