- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要实现一个带有多个按钮的弹出菜单。我已经创建了弹出窗口并向其中动态添加了按钮。现在我需要像 iPhone 主屏幕那样重新排列按钮。当出现长按手势时,按钮需要使用关闭按钮开始动画(就像我们尝试从手机中删除应用程序时出现的那样)。此外,我需要通过拖动来重新排列按钮的位置。我已经为按钮的外观和关闭按钮添加了动画,但我正在努力寻找一种重新排列按钮位置的方法。我搜索了很多,发现了很多关于此功能的链接,但其中大部分都非常冗长和复杂。我只想要一个可以快速实现的快速/简单的。任何想法家伙?
最佳答案
我有同样的问题,我用下面的代码解决了我的问题。
1) 从文档文件夹中获取所有图像
2) 在scrollview中设置
3) 将 UILongPressGestureRecognizer 放在 Imageview 上。并显示十字按钮。
4) 如果删除则重复步骤 2 到 4。
无需排列整个项目,只需从 ScrollView 中删除所有项目并重新填充即可。
//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];
}
}
关于iphone - 带有可重新排列按钮的 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17524128/
抱歉这个奇怪的标题,但是,这正是我所需要的。我知道这是可以做到的,因为我见过其他应用程序这样做。 基本上,我的应用程序有一个启用分页的大 ScrollView ,其中包含一些嵌套的 ScrollVie
我还没有尝试过,但我假设一旦我知道用户想要在子 ScrollView 中滚动,我就必须禁用父 ScrollView 的滚动,对吗? 两个 ScrollView 都是水平滚动的。 如何暂时禁用父级的滚动
我需要实现这个方法: - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center { 但我不知道如何获得我需要的中心坐标。通常
我想根据 UIAccelerometer 值滚动 ScrollView 。实现这一目标的最佳方法是什么? 最初,我根据获得的加速度计值将内容偏移设置为一个值。然后在每个 scrollViewDidEn
我有一个 UIScrollView,我需要底部淡入透明,这样它就不会突然切断内容。 UIScrollView 的背景是自定义颜色。这是我到目前为止所拥有的,但图层显示为白色,而不是自定义颜色。这就是我
我有一个 UIScrollviews 彼此存在的层次结构。 如何将特定区域的滑动事件重定向到内部 ScrollView ? 最佳答案 如果您将 UIScrollViews 设置为只能在一个轴或另一轴上
在我的应用程序中,我有一个扩展 UIScrollView 并在用户滚动时填充其内容的 View 。但是,如果用户滚动太快,则 UIScrollView 内填充的 View 不会及时创建,您实际上可以看
我有一个包含内容的 UIScroll View 。如果该内容溢出,使 ScrollView 可滚动,我想将 View 的底部设置为某种颜色。如果没有,我想将其设置为不同的颜色。 我的问题是,我不知道如
我有一个 ScrollView ,其中包含一个 UIImageView,以及一个额外的 UIScrollView 和 UIImageView。 我想要实现的是独立缩放和平移每个 ScrollView
类似的问题也有人问过这个,但我的困境其实和他们正好相反。我有一个全屏、分页 UIScrollView,每个页面都是屏幕的大小。在其中,我有一些页面本身就是 UIScrollViews,它们的宽度大于屏
我有一个 UIScrollView 和另一个 UIScrollView 。它们都水平滚动并且具有 pagingEnabled = YES。假设我开始滚动内部 ScrollView 并到达最右边界。如果
我有一个 UIScrollView,里面有一个(自定义)UIView。 在我的 scrollViewDidScroll 方法中,我正在调用 [myCustomView setNeedsDisplay]
我有一个水平滚动的 UICollectionView,其中填充了垂直滚动的 UITableViews(两者都是 UISCrollView 的子类)。当滚动手势开始沿任一方向滚动时,在其减速完成之前不会
iOS 8, swift 。我试图在水平 ScrollView 上创建垂直 ScrollView 。它们都是 UIScrollView。垂直 ScrollView 允许从底部向上滑动 View 。顶部
我需要一个分页的 UIScrollView,它以 UIScreenEdgePanGestureRecognizer 的方式检测屏幕边缘的平移,而不是像 UIPanGestureRecognizer 那
假设我有多个 ScrollView ,并且我希望在任何给定时间只打开其中一个 (scrollView.contentOffset != 0)。因此,当 scroll view 1 打开且 scroll
我在分页 UIScrollView(外部)中的 VC 中有 UIScrollView(内部 - UICollectionView 是诚实的)。两者都是水平的。当我向内滚动到边缘(左侧或右侧)时,它开始
如何在不干扰现有 UIScrollView 的情况下从现有 UIScrollView 获取重复的 UIScrollView。 第一个 ScrollView , UIScrollView *firstS
我的应用程序中有一个点,我删除了一个 ScrollView 并添加了另一个。我希望新 ScrollView 中也能出现上一个 ScrollView 中的相同手势。我尝试过: s
请告诉我如何在类似嵌套进程的 ScrollView 中设置 ScrollView 。 以下代码部分有效。 int x=10; int y=10; for(int i=0; i<5; i++) {
我是一名优秀的程序员,十分优秀!