如何在iOS中利用collectionView實現(xiàn)一個照片刪除功能-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)如何在iOS中利用collectionView實現(xiàn)一個照片刪除功能,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為大化企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站制作,大化網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

,工程圖。

如何在iOS中利用collectionView實現(xiàn)一個照片刪除功能

三,代碼。

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設(shè)置大小后,一行多少個cell,由cell的寬度決定
 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
 //設(shè)置cell的尺寸
 [flowLayout setItemSize:CGSizeMake(70, 70)];
 //設(shè)置其布局方向
 [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
 //設(shè)置其邊界(上,左,下,右)
 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個數(shù)
- (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) {
  // 如果是編輯狀態(tài)則取消編輯狀態(tài)
  [self cancelWobble];
 }else{
  //不是編輯狀態(tài),添加圖片
  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];
  }
  // 沒有刪除完,執(zhí)行晃動動畫
  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 {
   // 照片的元數(shù)據(jù)參數(shù)
   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];
}
// 判斷設(shè)備是否有攝像頭
- (BOOL) isCameraAvailable{
 return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
#pragma mark - 相冊文件選取相關(guān)
// 相冊是否可用
- (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實現(xiàn)一個照片刪除功能,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站bm7419.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁名稱:如何在iOS中利用collectionView實現(xiàn)一個照片刪除功能-創(chuàng)新互聯(lián)
文章位置:http://bm7419.com/article0/dcojoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站建站公司、網(wǎng)站策劃網(wǎng)站建設(shè)、企業(yè)建站自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化