ios 实现按钮组合增删
来源:互联网 发布:物联网云计算运用 编辑:程序博客网 时间:2024/06/08 07:21
上下两部分按钮组合,点击上部分的按钮,按钮移动到下部分;点击下部分中的按钮移动到上部分
页面组合底部是一个uiscrollview 上面两个view
view添加点击事件
//点击分类设置view 实现分类按钮view的显示和隐藏 self.showFenButton.userInteractionEnabled=YES; UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fenleiChanged)]; [self.showFenButton addGestureRecognizer:tapGesture];
NSInteger senderSwitch=1;-(void)fenleiChanged { if(senderSwitch==0) { btnView.hidden=false; [self.updown setImage:[UIImage imageNamed:@"arrow_down.png"]]; senderSwitch=1; } else { btnView.hidden=true; [self.updown setImage:[UIImage imageNamed:@"arrow_right.png"]]; senderSwitch=0; } }
按钮生成以及点击移动实现
点击返回后重新加载主页面-(void)showFenleiDetail{ hasdict=[NSMutableArray arrayWithCapacity:10]; nodict=[NSMutableArray arrayWithCapacity:10]; CGRect rect = [[UIScreen mainScreen] bounds]; CGSize size = rect.size; CGFloat width = size.width; n=width/100; NSInteger all=self.allcategory.count; NSInteger selected=self.selectedcategory.count; btnView=[[UIView alloc]initWithFrame:CGRectMake(0, 200, width, (all/n+selected/n)*30+150)]; for (int i=1; i<=selected/n+1; i++) { for (int j=1;j<=n; j++) { if (((i-1)*n +j)>selected+1) { break; } if (((i-1)*n +j)==selected+1) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5+105*(j-1), 30*(i-1), 100, 25)]; [button setTitle:@"标记位" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:14];//title字体大小 button.hidden=true; [button setTag:20001]; [btnView addSubview:button]; [hasdict addObject:button]; break; } UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5+105*(j-1), 30*(i-1), 100, 25)]; [button.layer setCornerRadius:5.0]; button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; button. contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter; [button setTitle:[NSString stringWithFormat:@"%@",[self.selectedcategory[((i-1)*n +j)-1] classNames]] forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:14];//title字体大小 button.titleLabel.textColor=[UIColor blackColor]; [button setImage:[UIImage imageNamed:@"chahao.png"] forState:UIControlStateNormal]; [button setImageEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 75)]; [button setBackgroundColor:[UIColor whiteColor]]; [button addTarget:self action:@selector(deleteFen:) forControlEvents:UIControlEventTouchUpInside]; [button setTag:[[self.selectedcategory[((i-1)*n +j)-1] classId ]intValue] ]; [hasdict addObject:button]; [btnView addSubview:button]; } } UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 30+((selected-1)/n)*30, 200, 35)]; label.text=@"点击添加关注分类"; [label setTag:30001]; label.textColor=[UIColor grayColor]; [btnView addSubview:label]; for (int i=1; i<=all/n+1; i++) { for (int j=1;j<=n; j++) { if (((i-1)*n +j)>all+1) { break; } if (((i-1)*n +j)==all+1) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5+105*(j-1), 50+label.frame.origin.y+30*(i-1), 100, 25)]; [button setTitle:@"标记位" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:14];//title字体大小 button.hidden=true; [button setTag:10001]; [btnView addSubview:button]; [nodict addObject:button]; break; } UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5+105*(j-1), 50+label.frame.origin.y+30*(i-1), 100, 25)]; [button.layer setCornerRadius:5.0]; button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; button. contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter; [button setTitle:[NSString stringWithFormat:@"%@",[self.allcategory[((i-1)*n +j)-1] classNames]] forState:UIControlStateNormal]; button.titleLabel.font = [UIFont systemFontOfSize:14]; [button setImage:[UIImage imageNamed:@"jiahao.png"] forState:UIControlStateNormal]; [button setImageEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 75)]; [button setBackgroundColor:[UIColor blueColor]]; [button addTarget:self action:@selector(addFen:) forControlEvents:UIControlEventTouchUpInside]; [button setTag:[[self.allcategory[((i-1)*n +j)-1] classId ]intValue] ]; [nodict addObject:button]; [btnView addSubview:button]; } } }//从已经配置的分类中删除-(void)deleteFen:(id)sender{ if (hasdict.count>2) { //后面的按钮向前移动 for (int i=0; i<hasdict.count; i++) { UIButton *btn=(UIButton *)hasdict[i]; if (btn.tag==[sender tag]) { CGRect temprect=btn.frame; CGRect temp2rect; for (int j=i+1; j<hasdict.count;j++) { UIButton *lastbtn=(UIButton *)hasdict[j]; temp2rect=lastbtn.frame; lastbtn.frame=temprect; temprect=temp2rect; } //已经配置的按钮数组中删除 [hasdict removeObject:btn]; //修改本地缓存中数据 [self.allcategory addObject:self.selectedcategory[i]]; [self.selectedcategory removeObject:self.selectedcategory[i]]; NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults]; [userDefaults setObject:self.selectedcategory forKey:@"UserCategory"]; [userDefaults setObject:self.allcategory forKey:@"AllCategory"]; [userDefaults synchronize]; break; } } //删除的分类移动到下面没有配置的分类中 UIButton *delButton=(UIButton *)sender; CGRect rect = [[UIScreen mainScreen] bounds]; CGSize size = rect.size; CGFloat width = size.width; UIView *button = [self.selectfenlei viewWithTag:10001]; CGRect btnFrame=button.frame; [nodict insertObject:delButton atIndex:nodict.count-1]; [delButton setFrame:btnFrame]; [delButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [delButton setImage:[UIImage imageNamed:@"jiahao.png"] forState:UIControlStateNormal]; [delButton setBackgroundColor:[UIColor blueColor]]; [delButton removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside]; [delButton addTarget:self action:@selector(addFen:) forControlEvents:UIControlEventTouchUpInside]; if (width-btnFrame.origin.x<200) { [button setFrame:CGRectMake(5, btnFrame.origin.y+30, btnFrame.size.width, btnFrame.size.height)]; }else{ [button setFrame:CGRectMake(btnFrame.origin.x+105, btnFrame.origin.y, btnFrame.size.width, btnFrame.size.height)]; } if (fmod(hasdict.count-1,n )==0) { //已经配置的按钮 减少来一行 将下面的控件上移 UIView *label = [self.selectfenlei viewWithTag:30001]; [label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y-30, label.frame.size.width, label.frame.size.height)]; for (int i=0; i<nodict.count; i++) { UIButton *btn=(UIButton *)nodict[i]; [btn setFrame:CGRectMake(btn.frame.origin.x, btn.frame.origin.y-30, btn.frame.size.width, btn.frame.size.height)]; } } }else{ MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeText; hud.labelText = @"请至少保留一个分类配置!"; hud.margin =15.f; hud.removeFromSuperViewOnHide = YES; hud.userInteractionEnabled=NO; [hud hide:YES afterDelay:3]; }}//配置添加-(void)addFen:(id)sender{ //后面的按钮向前移动 for (int i=0; i<nodict.count; i++) { UIButton *btn=(UIButton *)nodict[i]; if (btn.tag==[sender tag]) { CGRect temprect=btn.frame; CGRect temp2rect; for (int j=i+1; j<nodict.count;j++) { UIButton *lastbtn=(UIButton *)nodict[j]; temp2rect=lastbtn.frame; lastbtn.frame=temprect; temprect=temp2rect; } //未配置按钮数组中删除 [nodict removeObject:btn]; //修改本地缓存中数据 [self.selectedcategory addObject:self.allcategory[i]]; [self.allcategory removeObject:self.allcategory[i]]; NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults]; [userDefaults setObject:self.selectedcategory forKey:@"UserCategory"]; [userDefaults setObject:self.allcategory forKey:@"AllCategory"]; [userDefaults synchronize]; break; } } //点击的按钮向上移动到一配置的分类中 CGRect rect = [[UIScreen mainScreen] bounds]; CGSize size = rect.size; CGFloat width = size.width; UIView *button = [self.selectfenlei viewWithTag:20001]; CGRect btnFrame=button.frame; UIButton *addButton=(UIButton *)sender; [hasdict insertObject:addButton atIndex:hasdict.count-1]; [addButton setFrame:btnFrame]; [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [addButton setImage:[UIImage imageNamed:@"chahao.png"] forState:UIControlStateNormal]; [addButton setBackgroundColor:[UIColor whiteColor]]; [addButton removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside]; [addButton addTarget:self action:@selector(deleteFen:) forControlEvents:UIControlEventTouchUpInside]; if (width-btnFrame.origin.x<200) { [button setFrame:CGRectMake(5, btnFrame.origin.y+30, btnFrame.size.width, btnFrame.size.height)]; }else{ [button setFrame:CGRectMake(btnFrame.origin.x+105, btnFrame.origin.y, btnFrame.size.width, btnFrame.size.height)]; } if (fmod(hasdict.count-1,n )==1) { //已经配置的按钮 增加来一行 将下面的控件下移 UIView *label = [self.selectfenlei viewWithTag:30001]; [label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y+30, label.frame.size.width, label.frame.size.height)]; for (int i=0; i<nodict.count; i++) { UIButton *btn=(UIButton *)nodict[i]; [btn setFrame:CGRectMake(btn.frame.origin.x, btn.frame.origin.y+30, btn.frame.size.width, btn.frame.size.height)]; } }}
两组分类差集,需要加mutableCopy//点击返回操作后-(void)viewWillDisappear:(BOOL)animated{ UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; ScrollerViewController *mainList=[story instantiateViewControllerWithIdentifier:@"mainNavigation"]; [self presentViewController:mainList animated:NO completion:nil];}
self.selectedcategory =[[[CommenData alloc] getLocalSelectedFenlei]mutableCopy]; self.allcategory =[[[CommenData alloc] getLocalAllFenlei]mutableCopy]; [self.allcategory removeObjectsInArray:self.selectedcategory];
0 0
- ios 实现按钮组合增删
- iOS不规则按钮实现
- 自定义view继承viewgroup,实现组合按钮。
- iOS实现动态创建按钮
- iOS 点击按钮实现复制
- 按钮倒计时的实现 iOS
- iOS FMDB数据库实现增删改查
- 添加表格增删按钮
- MFC--快捷键(组合键/热键)与按钮实现操作按钮的方法
- MFC--快捷键(组合键/热键)与按钮实现操作按钮的方法
- MFC--快捷键(组合键/热键)与按钮实现操作按钮的方法
- iOS 代码实现圆角按钮
- iOS----四方块 动画按钮实现
- ios tableViewCell滑动实现多按钮
- iOS自定义按钮实现个性设置
- 实现类似IOS中滑动按钮效果
- iOS开发按钮点击操作的实现
- 一行代码巧妙实现iOS返回按钮
- iOS手机支付总结
- (转)JavaScript如何获得昨天明天等日期
- ios button 图标和文字位置设置
- Git 合并分支的代码
- ios tableview多种表格形式
- ios 实现按钮组合增删
- ios 屏幕适配
- ios设置点击状态栏返回到顶部
- 如题,既然直接对一个实例发送方法名就可以调用这个方法,那为什么还要使用performSelector:去调呢?两者u有啥区别
- ios左右滑动页面刷新
- 什么是RabbitMQ?
- java环境下:MongoDB的crud操作之Update (一)
- uilabel显示html
- shareSDK分享 没有安装qq 微信的时候