iOS 通过collectionView实现照片删除功能
程序员文章站
2024-02-12 16:32:46
一,效果图。
二,工程图。
三,代码。
viewcontroller.h
#import
@inter...
一,效果图。
二,工程图。
三,代码。
viewcontroller.h
#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
//点击添加按钮的时候,停止删除。 #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
#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
#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实现照片删除功能,希望对大家有所帮助
推荐阅读
-
iOS 通过collectionView实现照片删除功能
-
iOS开发之tableView实现左滑删除功能
-
iOS中tableview实现编辑、全选及删除等功能的方法示例
-
iOS实现UITableView左滑删除复制即用功能
-
iOS中tableview实现编辑、全选及删除等功能的方法示例
-
iPhone/iPad开发通过LocalNotification实现iOS定时本地推送功能
-
iOS实现UITableView左滑删除复制即用功能
-
iOS 通过collectionView实现照片删除功能
-
iPhone/iPad开发通过LocalNotification实现iOS定时本地推送功能
-
iOS实现通过按钮添加和删除控件的方法