我有同样的问题,我用下面的代码解决了我的问题。
1) 从 Document 文件夹中获取所有图片
2) 在滚动视图中设置它
3) 将 UILongPressGestureRecognizer 放在 Imageview 上。并显示十字按钮。
4) 如果删除,则重复步骤 2 至 4。
无需安排整个项目,只需从滚动视图中删除所有项目并重新填充即可。
//Document Directory
#define kAppDirectoryPath NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
#pragma mark - File Functions - Document/Cache Directory Functions
- (void)createDocumentDirectory:(NSString*)pStrDirectoryName
{
NSString *dataPath = [self getDocumentDirectoryPath:pStrDirectoryName];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:NULL];
}
- (NSString*)getDocumentDirectoryPath:(NSString*)pStrPathName
{
NSString *strPath = @"";
if(pStrPathName)
strPath = [[kAppDirectoryPath objectAtIndex:0] stringByAppendingPathComponent:pStrPathName];
return strPath;
}
-(void)setScrollviewItem {
NSArray* subviews = [[NSArray alloc] initWithArray: scrollObj.subviews];
for (UIView* view in subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
if ([view isKindOfClass:[UIButton class]]) {
[view removeFromSuperview];
}
}
[subviews release];
[arrSaveImage removeAllObjects];
NSError *error = nil;
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[slef getDocumentDirectoryPath:@"MyPhotos"] error:&error];// MyPhoto is my Directory Name.
if (!error) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *imagesOnly = [dirContents filteredArrayUsingPredicate:predicate];
for (int i=0;i<[imagesOnly count]; i++) {
[arrSaveImage addObject:[imagesOnly objectAtIndex:i]];
}
}
int px=0;
for (int i = 0; i < [arrSaveImage count]; i++) {
UIImageView *imgBackScroll=[[UIImageView alloc] init];
NSString *strPath=[self getDocumentDirectoryPath:@"MyPhotos"];
strPath=[NSString stringWithFormat:@"%@/%@",strPath,[arrSaveImage objectAtIndex:i]];
imgBackScroll.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:strPath]];
imgBackScroll.frame=CGRectMake(px+5, 10, 90, 80);
imgBackScroll.layer.cornerRadius = 5.0;
imgBackScroll.layer.masksToBounds = YES;
imgBackScroll.tag=i;
imgBackScroll.userInteractionEnabled = YES;
[scrollObj addSubview:imgBackScroll];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];//This is your Cross delete button on corner
//[btn setTitle:@"-" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"CrossDelete.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(deleteButton:) forControlEvents:UIControlEventTouchUpInside];
btn.frame=CGRectMake(imgBackScroll.frame.origin.x-2, 0, 15, 15);
btn.hidden=YES;
btn.tag=i;
[scrollObj addSubview:btn];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(startWobbling:)];
longPressGesture.view.tag=i;
[imgBackScroll addGestureRecognizer:longPressGesture];
[imgBackScroll release];
[longPressGesture release];
px=px+95;
}
scrollObj.contentSize = CGSizeMake(px, scrollObj.frame.size.height);
}
-(void) startWobbling:(UILongPressGestureRecognizer*)gesture{
CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-5.0));
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(5.0));
gesture.view.transform = leftWobble; // starting point
[UIView beginAnimations:@"wobble" context:gesture.view];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:11];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)];
gesture.view.transform = rightWobble; // end here & auto-reverse
[UIView commitAnimations];
NSArray* subviews = [[NSArray alloc] initWithArray: scrollObj.subviews];
for (UIView* view in subviews) {
if ([view isKindOfClass:[UIButton class]]) {
if (view.tag==gesture.view.tag) {
view.hidden=NO;
}
}
}
[subviews release];
}
//When Delete button pressed
-(IBAction)deleteButton:(id)sender {
UIButton *bt=(UIButton *)sender;
self.strDeleteFilePath=[FunctionManager getDocumentDirectoryPath:@"MyPhotos"];
self.strDeleteFilePath=[NSString stringWithFormat:@"%@/%@",strDeleteFilePath,[arrSaveImage objectAtIndex:bt.tag]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Are you sure you want to delete this photo" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if(![fileManager removeItemAtPath:self.strDeleteFilePath error:&error]) {
NSLog(@"Delete failed:%@", error);
} else {
NSLog(@"image removed: %@",strDeleteFilePath);
}
[self setScrollviewItem];
}
}