亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > iOS > 正文

iOS 通過collectionView實現照片刪除功能

2019-10-21 18:43:40
字體:
來源:轉載
供稿:網友

一,效果圖。

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;}@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實現照片刪除功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
夜夜嗨av一区二区三区免费区| 欧美精品www| 久久影院免费观看| 亚洲精品在线91| 日韩av在线免费| 亚洲欧美日韩中文在线制服| 亚洲自拍偷拍福利| 欧美成人黄色小视频| 久久精品国产99国产精品澳门| 91精品国产色综合| 奇米4444一区二区三区| 91高清免费在线观看| 色播久久人人爽人人爽人人片视av| 国产精品自拍视频| 日韩亚洲成人av在线| 在线视频日韩精品| 在线成人中文字幕| 国内揄拍国内精品少妇国语| 国产视频精品va久久久久久| 日韩在线视频二区| 7m第一福利500精品视频| 亚洲精品视频网上网址在线观看| 亚洲91精品在线| 高清在线视频日韩欧美| 久久精品免费电影| 精品无人区太爽高潮在线播放| 高清欧美性猛交| 亚洲欧美中文另类| 欧美特黄级在线| 国产午夜一区二区| 欧美性xxxxx极品娇小| 国产97在线视频| 亚洲a中文字幕| 日韩亚洲精品视频| 国产精品狼人色视频一区| 欧美在线xxx| 国产精品久久久久av免费| 日本久久精品视频| 亚洲国产第一页| 日韩av成人在线| 国产精品99久久久久久www| 精品成人乱色一区二区| 亚洲视频在线播放| 亚洲大尺度美女在线| 欧美性猛交xxxx乱大交蜜桃| 日韩欧美福利视频| 国产成人久久久精品一区| 久久久精品2019中文字幕神马| 国产91精品久| 亚洲一区二区少妇| 国产一区二区三区在线| 欧美日韩国产va另类| 久久久99久久精品女同性| 亚洲国产成人久久综合一区| 91大神在线播放精品| 国产精品成人aaaaa网站| 青青a在线精品免费观看| 久久国产精品视频| 欧美另类xxx| 91av免费观看91av精品在线| 精品国产乱码久久久久久虫虫漫画| 高清日韩电视剧大全免费播放在线观看| 国内精品久久久久久影视8| 色综合伊人色综合网| 亚洲精品福利视频| 中文字幕欧美亚洲| 久久久久这里只有精品| 久久人人爽人人| 97国产在线视频| 国产精品久久久久久中文字| 91在线中文字幕| 国产精品美女主播在线观看纯欲| 亚洲成年人在线播放| 久久国产精品亚洲| 欧美极品少妇xxxxx| 成人免费福利在线| 亚洲aa中文字幕| 亚洲女同性videos| 亚洲无亚洲人成网站77777| 国产欧美日韩精品丝袜高跟鞋| 成人久久久久久久| 伊人激情综合网| 亚洲精品日韩在线| 性夜试看影院91社区| 97久久超碰福利国产精品…| 久久天天躁狠狠躁夜夜爽蜜月| 久久久久亚洲精品国产| 亚洲高清av在线| 亚洲毛片在线观看| 欧美精品久久一区二区| 久久久久久91香蕉国产| 久久久在线视频| 午夜精品免费视频| 8x海外华人永久免费日韩内陆视频| 亚洲伦理中文字幕| 综合国产在线视频| 亚洲国产一区二区三区在线观看| 在线播放亚洲激情| 97在线免费观看视频| 国产亚洲欧美日韩一区二区| 夜夜嗨av色综合久久久综合网| 国内外成人免费激情在线视频网站| 成人情趣片在线观看免费| 色视频www在线播放国产成人| 精品欧美一区二区三区| 欧美中文字幕第一页| 亚洲电影免费在线观看| 91免费国产视频| 久久99久久久久久久噜噜| 欧美人与性动交| 视频一区视频二区国产精品| 国模极品一区二区三区| 久久亚洲精品国产亚洲老地址| 久久久免费观看| 6080yy精品一区二区三区| 欧美激情a∨在线视频播放| 国内自拍欧美激情| 亚洲成人久久久久| 国产一区二区三区丝袜| 欧美在线观看www| 色综合视频一区中文字幕| 国产一区二区三区在线观看视频| 色婷婷av一区二区三区在线观看| 国产精品18久久久久久麻辣| 亚洲iv一区二区三区| 精品国内产的精品视频在线观看| 黑人巨大精品欧美一区免费视频| 国产性色av一区二区| 国产欧美一区二区三区在线| zzjj国产精品一区二区| 国产精品久久久久久久久粉嫩av| 这里只有视频精品| 国产成人精品国内自产拍免费看| 91国自产精品中文字幕亚洲| 日本亚洲欧美成人| 美女999久久久精品视频| 韩国三级日本三级少妇99| 91精品久久久久久| 蜜臀久久99精品久久久无需会员| 欧美成人午夜激情在线| 亚洲欧美中文日韩在线v日本| 日韩中文字幕在线播放| 美女性感视频久久久| 欧美人成在线视频| 97视频在线观看亚洲| 91免费看国产| 久久久成人精品| 亚洲女成人图区| 97视频人免费观看| 欧美电影第一页| 精品亚洲一区二区三区在线观看| 亚洲男人天堂网站| 久久国产精品视频| 亚洲国产精品va在线观看黑人| 亚洲国产欧美精品| 亚洲精品一区久久久久久| 国产精品久久久久久影视| 中文字幕欧美日韩| 午夜精品一区二区三区在线视频| 日韩中文在线不卡| 少妇精69xxtheporn| 国产日韩欧美电影在线观看| 日韩精品极品在线观看播放免费视频| 日韩av综合网站|