一,效果图。

二,工程图。

三,代码。

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. 基础I/O接口与操作

    C语言中的文件接口 1 打开文件 FILE * fopen(const char * path,const char * mode) 参数:path:文件路径 mode:打开文件的方式 返回值:成功返 ...

  2. MVC3学习:实现简单的相册管理和图片管理

    相册管理说白了就是文件夹管理,因此要用到命名空间using System.IO; 一.先来做相册管理,添加相册我就不做了,就是添加文件夹,这里主要做一下相册的显示.相册在页面上显示,需要一张图片,可以 ...

  3. tensorflow进阶篇-5(反向传播1)

    这里将讲解tensorflow是如何通过计算图来更新变量和最小化损失函数来反向传播误差的:这步将通过声明优化函数来实现.一旦声明好优化函数,tensorflow将通过它在所有的计算图中解决反向传播的项 ...

  4. python中@staticmethod与@classmethod

    @ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...

  5. Android内存管理篇 - 从updateOomAdjLocked看lowmemorykiller之外的Android进程回收机制

    提起android的进程回收机制,大家所熟知的是Android的lowmemroykiller的机制.当系统可用内存低于某个阀值时,即会杀死这个阀值对应的Adj值的所有应用.但是本篇文章并为是要介绍L ...

  6. 高可用Hadoop平台-答疑篇

    1.概述 这篇博客不涉及到具体的编码,只是解答最近一些朋友心中的疑惑.最近,一些朋友和网友纷纷私密我,我总结了一下,疑问大致包含以下几点: 我学 Hadoop 后能从事什么岗位? 在遇到问题,我该如何 ...

  7. 第三方登录:微信扫码登录(OAuth2.0)

    1.OAuth2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户提供 ...

  8. 【详解】JNI (Java Native Interface) (二)

    案例二:传递参数给C代码,并从其获取结果 注:这里传递的参数是基本类型的参数,在C代码中有直接的映射类型. 此案例所有生成的所有文件如下: (1)编写案例二的Java代码,如下: 这里我们定义了一个n ...

  9. 熟悉DAO模式的用法

    今天主要是使用DAO模式. DAO模式通过对业务层提供数据抽象层接口,实现了以下目标: 1. 数据存储逻辑的分离 通过对数据访问逻辑进行抽象,为上层机构提供抽象化的数据访问接口.业务层无需关心具体的s ...

  10. Spring中使用变量${}的方式进行参数配置

    在使用Spring时,有些情况下,在配置文件中,需要使用变量的方式来配置bean相关属性信息,比如下面的数据库的连接使用了${}的方式进行配置,如下所示: <bean id="data ...