一,效果图。

二,工程图。

三,代码。

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实现照片删除的更多相关文章

  1. iOS 开发之照片框架详解(2)

    一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...

  2. iOS 开发之照片框架详解(1)

    http://kayosite.com/ios-development-and-detail-of-photo-framework.html/comment-page-1 一. 概要 在 iOS 设备 ...

  3. iOS 开发之照片框架详解

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...

  4. iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...

  5. 笔记-iOS 视图控制器转场详解(上)

    这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...

  6. IOS开发笔记 IOS如何访问通讯录

    IOS开发笔记  IOS如何访问通讯录 其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的. 幸好ios6.0 以后给了个权限控制.当打开app的时候你可以选择拒绝. 实现方法: [plain] ...

  7. iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)

    本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...

  8. DW网页代码笔记

    DW网页代码笔记 1.样式.       class  插入类样式  标签技术(html)解决页面的内容样式技术(css)解决页面的外观脚本技术       解决页面动态交互问题<form> ...

  9. 前端学习:JS(面向对象)代码笔记

    前端学习:JS(面向对象)代码笔记 前端学习:JS面向对象知识学习(图解) 创建类和对象 创建对象方式1调用Object函数 <body> </body> <script ...

随机推荐

  1. Vue2.5开发去哪儿网App 第三章笔记 上

    1.  vue 生命周期函数 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后在数据变化时更新 ...

  2. Oracle VM VirtualBox启动后莫名奇妙的报错

    VirtualBox软件无法启动: 参考解决:http://blog.csdn.net/a_ssimi/article/details/52002939 修改兼容性:http://blog.csdn. ...

  3. 【原创】SQL Server 性能调优读书笔记

    CPU 100%: 有时可能是硬盘性能不足,或者内存容量不够,让CPU一直忙于I/O. 导致性能问题的一些因素: 用户习惯:在运行尖峰时刻做一些不必做但消耗资源的事情,如之行数据库完整备份,如在服务器 ...

  4. 鼠标右键添加"在此处打开命令窗口"

    从windows7开始,提供了一个便于从当前文件夹打开cmd命令行窗口的快捷方式: 直接使用  Shift+鼠标右键==>“在此处打开命令行窗口” 有可能以后会遇到的问题,可参考如下.. htt ...

  5. C#控件事件属性大全

    C#控件及常用设计整 1.窗体... 1 2.Label 控件... 3 3.TextBox 控件... 4 4.RichTextBox控件... 5 5.NumericUpDown 控件... 7 ...

  6. 最大子数组问题/Maximum Subarray

    问题描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  7. 《松本行弘的程序世界》读书笔记(上)——面向对象、程序块、设计模式、ajax

    1. 前言 半个月之前买了这本书,还是经园子里的一位网友推荐的.到现在看了一半多,基础的都看完了,剩下的几章可做高级部分来看.这本书看到现在,可以说感触很深,必须做一次读书笔记! 关于这本书,不了解的 ...

  8. Selenium3自动化问题二:各chrome版本对应的chromedriver版本

    一:问题说明 最近用到selenium3在火狐浏览器中执行自动化脚本,遇到了一些问题,最后解决方案中占比最多的就是浏览器和驱动版本不一致导致的,故这里给出chrome.firefox驱动的不同版本对应 ...

  9. Tomcat学习总结(9)——Apache Tomcat 8新特性

    一.Apache Tomcat 8介绍 Apache Tomcat 8RC1版于2013年8月份发布.它 经过了2年的开发,引入了很多新特征,由于目前还只是Alpha版,故不推荐在产品中使用.但是我们 ...

  10. GoogLeNetv3 论文研读笔记

    Rethinking the Inception Architecture for Computer Vision 原文链接 摘要 卷积网络是目前最新的计算机视觉解决方案的核心,对于大多数任务而言,虽 ...