ios 实现按钮组合增删

来源:互联网 发布:物联网云计算运用 编辑:程序博客网 时间:2024/06/08 07:21

上下两部分按钮组合,点击上部分的按钮,按钮移动到下部分;点击下部分中的按钮移动到上部分

ios 实现按钮组合增删 - wanglj7525 - 博客
 页面组合底部是一个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)];        }    }}


点击返回后重新加载主页面
//点击返回操作后-(void)viewWillDisappear:(BOOL)animated{     UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];    ScrollerViewController *mainList=[story instantiateViewControllerWithIdentifier:@"mainNavigation"];    [self presentViewController:mainList animated:NO completion:nil];}


两组分类差集,需要加mutableCopy
    self.selectedcategory =[[[CommenData alloc] getLocalSelectedFenlei]mutableCopy];    self.allcategory =[[[CommenData alloc] getLocalAllFenlei]mutableCopy];    [self.allcategory removeObjectsInArray:self.selectedcategory];
0 0
原创粉丝点击