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

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

服务器之家 - 编程语言 - IOS - iOS 通过collectionView实现照片删除功能

iOS 通过collectionView实现照片删除功能

2021-04-06 15:21陈沐夕 IOS

这篇文章主要介绍了iOS 通过collectionView实现照片删除功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友可以参考下

一,效果图。

iOS 通过collectionView实现照片删除功能

二,工程图。

iOS 通过collectionView实现照片删除功能

三,代码。

viewcontroller.h

?
1
2
3
4
5
6
7
8
9
10
11
12
#import <uikit/uikit.h>
@interface viewcontroller : uiviewcontroller
<uicollectionviewdatasource,uicollectionviewdelegate,uicollectionviewdelegateflowlayout,uialertviewdelegate,uiactionsheetdelegate,uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate>
{
 uicollectionview *_collectionview;
 uiimagepickercontroller *_imagepicker;
 nsmutablearray *photos;
 nsmutablearray *dataarray;
 nsinteger deleteindex;
 bool wobble;
}
@end

viewcontroller.m

?
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//点击添加按钮的时候,停止删除。
#import "viewcontroller.h"
#import "photocollectionviewcell.h"
nsinteger const photo = 8;
@interface viewcontroller ()
@end
@implementation viewcontroller
- (void)viewdidload {
 [super viewdidload];
 // do any additional setup after loading the view, typically from a nib.
 //其布局很有意思,当你的cell设置大小后,一行多少个cell,由cell的宽度决定
 uicollectionviewflowlayout *flowlayout = [[uicollectionviewflowlayout alloc]init];
 //设置cell的尺寸
 [flowlayout setitemsize:cgsizemake(70, 70)];
 //设置其布局方向
 [flowlayout setscrolldirection:uicollectionviewscrolldirectionvertical];
 //设置其边界(上,左,下,右)
 flowlayout.sectioninset = uiedgeinsetsmake(5,5,5,5);
 _collectionview = [[uicollectionview alloc]initwithframe:cgrectmake(10, 50, 320,85*2) collectionviewlayout:flowlayout];
 _collectionview.datasource = self;
 _collectionview.delegate = self;
 _collectionview.backgroundcolor = [uicolor redcolor];
 [_collectionview registerclass:[photocollectionviewcell class] forcellwithreuseidentifier:@"photo"];
 [self.view addsubview:_collectionview];
 photos = [[nsmutablearray alloc ] init];
 dataarray = [[nsmutablearray alloc ] init];
 [dataarray addobject:[uiimage imagenamed:@"contract_addpic1"]];
}
//section
- (nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview
{
 return 1;
}
//item个数
- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section
{
 return dataarray.count;
}
-(uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath
{
 nslog(@"--indexpath.row--%ld",indexpath.row);
 nslog(@"---indexpath.section--%ld",indexpath.section);
 photocollectionviewcell *cell = (photocollectionviewcell *)[collectionview dequeuereusablecellwithreuseidentifier:@"photo" forindexpath:indexpath];
 cell.tag=indexpath.row;
 //图片
 cell.photoimage.image=dataarray[indexpath.row];
 // 删除按钮
 cell.deletebtn.tag =indexpath.row;
 cell.deletebtn.hidden=yes;
 [cell.deletebtn addtarget:self action:@selector(doclickdeletebutton:) forcontrolevents:uicontroleventtouchupinside];
 //增加按钮
 if (indexpath.row == dataarray.count -1) {
  cell.addbtn.hidden = no;
 }else
 {
  cell.addbtn.hidden = yes;
 }
 [cell.addbtn addtarget:self action:@selector(doclickaddbutton:) forcontrolevents:uicontroleventtouchupinside];
 // 长按删除
 uilongpressgesturerecognizer *longpress = [[uilongpressgesturerecognizer alloc ] initwithtarget:self action:@selector(longpressedaction)];
 [cell.contentview addgesturerecognizer:longpress];
 return cell;
}
#pragma -mark -doclickactions
//删除按钮
-(void)doclickdeletebutton:(uibutton *)btn
{
 nslog(@"-----doclickdeletebutton-------");
 uialertview *alert = [[uialertview alloc ] initwithtitle:@"提示" message:@"您确定要删除吗?" delegate:self cancelbuttontitle:@"取消" otherbuttontitles:@"确定", nil];
 deleteindex = btn.tag;
 [alert show];
 nslog(@"---delete--dataarray---%@",dataarray);
}
//增加按钮
-(void)doclickaddbutton:(uibutton *)btn
{
 nslog(@"-----doclickaddbutton-------");
 if (wobble) {
  // 如果是编辑状态则取消编辑状态
  [self cancelwobble];
 }else{
  //不是编辑状态,添加图片
  if (dataarray.count > photo) {
   uialertview *alert = [[uialertview alloc ] initwithtitle:@"提示" message:@"最多支持8个" delegate:self cancelbuttontitle:@"取消" otherbuttontitles:@"确定", nil];
   [alert show];
  }else
  {
   uiactionsheet *actionsheet = [[uiactionsheet alloc]
           initwithtitle:nil
           delegate:(id)self
           cancelbuttontitle:@"取消"
           destructivebuttontitle:nil
           otherbuttontitles:@"拍照", @"我的相册",nil];
   actionsheet.actionsheetstyle = uiactionsheetstyleblackopaque;
   [actionsheet showinview:self.view];
  }
 }
 nslog(@"---add--dataarray---%@",dataarray);
}
//长按删除
-(void)longpressedaction
{
 nslog(@"-----longpressedaction-------");
 wobble = yes;
 nsarray *array = [_collectionview subviews];
 for (int i = 0; i < array.count; i ++) {
   if ([array[i] iskindofclass:[photocollectionviewcell class]]) {
   photocollectionviewcell *cell = array[i];
   if (cell.addbtn.hidden) {
    cell.deletebtn.hidden = no;
   }
   else
   {
    cell.deletebtn.hidden = yes;
    cell.photoimage.image = [uiimage imagenamed:@"ensure"];
    cell.tag = 999999;
   }
   // 晃动动画
   [self animationviewcell:cell];
  }
 }
}
// 取消晃动
-(void)cancelwobble
{
 wobble = no;
 nsarray *array = [_collectionview subviews];
 for (int i = 0; i < array.count; i ++) {
  if ([array[i] iskindofclass:[photocollectionviewcell class]]) {
   photocollectionviewcell *cell = array[i];
   cell.deletebtn.hidden = yes;
   if (cell.tag == 999999) {
    cell.photoimage.image = [uiimage imagenamed:@"plus"];
   }
   // 晃动动画
   [self animationviewcell:cell];
  }
 }
}
// 晃动动画
-(void)animationviewcell:(photocollectionviewcell *)cell
{
 //摇摆
 if (wobble){
  cell.transform = cgaffinetransformmakerotation(-0.1);
  [uiview animatewithduration:0.08
        delay:0.0
       options:uiviewanimationoptionrepeat|uiviewanimationoptionautoreverse|uiviewanimationoptionallowuserinteraction|uiviewanimationoptioncurvelinear
       animations:^{
        cell.transform = cgaffinetransformmakerotation(0.1);
       } completion:nil];
 }
 else{
  [uiview animatewithduration:0.25
        delay:0.0
       options:uiviewanimationoptionallowuserinteraction|uiviewanimationoptionbeginfromcurrentstate|uiviewanimationoptioncurveeaseout
       animations:^{
        cell.transform = cgaffinetransformidentity;
       } completion:nil];
 }
}
#pragma -mark -uiactionsheetdelegate
- (void)actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex
{
 if (buttonindex == 0) {
  [self opencamera];
 }else if(buttonindex == 1) {
  [self openpics];
 }
}
#pragma -mark -uialertviewdelegate
- (void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)buttonindex
{
 if (buttonindex == 1) {
  [dataarray removeobjectatindex:deleteindex];
  nsindexpath *path = [nsindexpath indexpathforrow:deleteindex insection:0];
  [_collectionview deleteitemsatindexpaths:@[path]];
  // 如果删除完,则取消编辑
  if (dataarray.count == 1) {
   [self cancelwobble];
  }
  // 没有删除完,执行晃动动画
  else
  {
   [self longpressedaction];
  }
 }
}
#pragma -mark -camera
// 打开相机
- (void)opencamera {
 if ([uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera])
 {
  if (_imagepicker == nil) {
   _imagepicker = [[uiimagepickercontroller alloc] init];
  }
  _imagepicker.delegate = (id)self;
  _imagepicker.sourcetype = uiimagepickercontrollersourcetypecamera;
  _imagepicker.showscameracontrols = yes;
  _imagepicker.allowsediting = yes;
  [self.navigationcontroller presentviewcontroller:_imagepicker animated:yes completion:nil];
 }
}
// 打开相册
- (void)openpics {
 if (_imagepicker == nil) {
  _imagepicker = [[uiimagepickercontroller alloc] init];
 }
 _imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
 _imagepicker.allowsediting = yes;
 _imagepicker.delegate = (id)self;
 [self presentviewcontroller:_imagepicker animated:yes completion:null];
}
// 选中照片
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info{
 nsstring *mediatype = [info objectforkey:uiimagepickercontrollermediatype];
 [_imagepicker dismissviewcontrolleranimated:yes completion:null];
 _imagepicker = nil;
 // 判断获取类型:图片
 if ([mediatype isequaltostring:@"public.image"]){
  uiimage *theimage = nil;
  // 判断,图片是否允许修改
  if ([picker allowsediting]){
   //获取用户编辑之后的图像
   theimage = [info objectforkey:uiimagepickercontrollereditedimage];
  } else {
   // 照片的元数据参数
   theimage = [info objectforkey:uiimagepickercontrolleroriginalimage] ;
  }
  [dataarray insertobject:theimage atindex:0];
  nsindexpath *path = [nsindexpath indexpathforrow:0 insection:0];
  [_collectionview insertitemsatindexpaths:@[path]];
 }
}
- (void)imagepickercontrollerdidcancel:(uiimagepickercontroller *)picker {
 [picker dismissviewcontrolleranimated:yes completion:null];
}
// 判断设备是否有摄像头
- (bool) iscameraavailable{
 return [uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypecamera];
}
#pragma mark - 相册文件选取相关
// 相册是否可用
- (bool) isphotolibraryavailable{
 return [uiimagepickercontroller issourcetypeavailable: uiimagepickercontrollersourcetypephotolibrary];
}
- (void)didreceivememorywarning {
 [super didreceivememorywarning];
 // dispose of any resources that can be recreated.
}
@end

photocollectionviewcell.h

?
1
2
3
4
5
6
#import <uikit/uikit.h>
@interface photocollectionviewcell : uicollectionviewcell
@property (weak, nonatomic) iboutlet uibutton *addbtn;
@property (weak, nonatomic) iboutlet uiimageview *photoimage;
@property (weak, nonatomic) iboutlet uibutton *deletebtn;
@end

photocollectionviewcell.m

?
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
#import "photocollectionviewcell.h"
@implementation photocollectionviewcell
- (id)initwithframe:(cgrect)frame
{
 self = [super initwithframe:frame];
 if (self)
 {
  // 初始化时加载collectioncell.xib文件
  nsarray *arrayofviews = [[nsbundle mainbundle] loadnibnamed:@"photocollectionviewcell" owner:self options:nil];
  // 如果路径不存在,return nil
  if (arrayofviews.count < 1)
  {
   return nil;
  }
  // 如果xib中view不属于uicollectionviewcell类,return nil
  if (![[arrayofviews objectatindex:0] iskindofclass:[uicollectionviewcell class]])
  {
   return nil;
  }
  // 加载nib
  self = [arrayofviews objectatindex:0];
 }
 return self;
}
- (void)awakefromnib {
 // initialization code
}
@end

总结

以上所述是小编给大家介绍的ios 通过collectionview实现照片删除功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/yang-guang-girl/archive/2017/11/23/7883117.html

延伸 · 阅读

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

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

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

    苦练内功5832021-04-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

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

    SimpleWorld11022021-01-28
  • IOS关于iOS自适应cell行高的那些事儿

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

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

    daisy6092021-05-17
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

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

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

    xiari5772021-06-01
  • IOSiOS通过逆向理解Block的内存模型

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

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

    Swiftyper12832021-03-03
  • IOSiOS布局渲染之UIView方法的调用时机详解

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

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

    windtersharp7642021-05-04
  • IOS解析iOS开发中的FirstResponder第一响应对象

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

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

    一片枫叶4662020-12-25
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

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

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

    J_Kang3862021-04-22