当前位置: 首页 > 编程笔记 >

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

乐正宏深
2023-03-14
本文向大家介绍IOS 开发之网络图片轮播图的实现,包括了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 

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

 类似资料:
  • 图片轮播继承自slide插件,因此其DOM结构、事件均和slide插件相同; DOM结构默认不支持循环播放,DOM结构如下: <div class="mui-slider"> <div class="mui-slider-group"> <div class="mui-slider-item"><a href="#"><img src="1.jpg" /></a></div>

  • 本文向大家介绍Bootstrap开发实战之响应式轮播图,包括了Bootstrap开发实战之响应式轮播图的使用技巧和注意事项,需要的朋友参考一下 本文主要内容是在导航条的下方做一张轮播图,自动播放最新的重要动态,供大家参考,具体内容如下 一、响应式轮播图 //响应式轮播图 //所需要的 jQuery 控制 //调整轮播器箭头位置 //所需要的 CSS 如果大家还想深入学习,可以点击这里进行学习,再为

  • 本文向大家介绍Android开发之滑动图片轮播标题焦点,包括了Android开发之滑动图片轮播标题焦点的使用技巧和注意事项,需要的朋友参考一下 先给大家这是下效果图: 谷歌提供的v4包,ViewPager 在布局文件中,先添加<android.support.v4.view.ViewPager/>控件,这个只是轮播的区域 在布局文件中,布置标题描述部分 线性布局,竖向排列,背景色黑色半透明,这个布

  • 本文向大家介绍Android开发实现的自动换图片、轮播图效果示例,包括了Android开发实现的自动换图片、轮播图效果示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android开发实现的自动换图片、轮播图效果。分享给大家供大家参考,具体如下: 50行代码急速实现轮播图: 先看效果: 两个例子没太大区别, 不过i是布局文件中控件的Height不一样罢了这里一第二个为例: 嗯好像是五十

  • 本文向大家介绍js手动播放图片实现图片轮播效果,包括了js手动播放图片实现图片轮播效果的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了js图片轮播具体实现代码,供大家参考,具体内容如下 一、html代码部分(et.thtml): 二、css代码部分(styleet,css): 三、js代码部分(et.js): 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教

  • 图片轮播(bui-image-slider)     用法 <bui-image-slider :items="itemList" @itemClick="onItemClick" @change="onChange"> </bui-image-slider> data () { return { itemList: [ {