服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - IOS 开发之网络图片轮播图的实现

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

2021-03-29 15:10水桶前辈 IOS

这篇文章主要介绍了IOS 开发之网络图片轮播图的实现的相关资料,希望通过此文大家能够掌握轮播图的实现,需要的朋友可以参考下

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

截图

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

1.使用

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

2.源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#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

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

原文链接:http://blog.csdn.net/robinson_911/article/details/76038882

延伸 · 阅读

精彩推荐
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17