 
         
         
         
         
        版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】如何在iOS中利用collectionView实现一个照片删除功能
本篇文章给大家分享的是有关如何在iOS中利用collectionView实现一个照片删除功能,在下觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着在下一起来看看吧。,工程图。三,代码。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;
}
@endViewController.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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 消化内镜治疗围手术期管理与护理考核试题及答案
- 2025年检验科新入职人员岗前院感培训考核试题及答案
- 预算编制与管理标准化工具集
- 2025年安全培训试卷及答案:煤矿提升机操作员特种设备安全操作考核
- 企业信息档案管理体系构建工具集及规范说明
- 团队协作自动化文档编写工具
- 财产估值客观性保证承诺书6篇
- 财务报表编制模板与规范
- 商品售后放心承诺书(8篇)
- 企业员工绩效考核指标库模板
- 2025年福建省国有资产管理有限公司人员招聘笔试备考试题及答案
- 《供配电技术及设备》课件-第四章 低压电气元件及成套设备
- 疼痛护理中医课件
- 临床医学病例分析标准流程
- 《财政支出效益分析》课件
- 骨质疏松症的治疗进展与新型药物研究
- 重庆市交通建设工程危险性较大分部分项工程安全管理实施细则
- 体检报告管理制度
- 采血后预防淤青的按压方式
- 2025年药物外渗考试题及答案
- 水利工程施工阶段减碳措施与碳排放管理研究
 
            
评论
0/150
提交评论