iOS實現通過按鈕添加和刪除控件的方法

這篇文章主要爲大家詳細介紹了iOS通過按鈕添加和刪除控件的方法,具有一定的參考價值,感興趣的小夥伴們可以參考一下

本文實例爲大家分享了iOS通過按鈕添加和刪除控件,供大家參考,具體內容如下

想要達到的效果如下:

先講一下這個demo主要部分,即通過按鈕實現增刪圖標

分析:

1、每一個圖標需要兩個數據,即圖片和描述用的字符串 ,所以創建一個Item類來封裝從plist文件讀取出來的數據:

1)plist文件如下:

2)Item類:

.h文件

#import <Foundation/Foundation.h>

@interface Item : NSObject
//描述的字符串
@property(nonatomic,copy)NSString * desStr;
//圖片路徑
@property(nonatomic,copy)NSString * imgPath;

-(instancetype)initWithString:(NSString *)desStr andimgPath:(NSString *)imgPath;

@end

.m文件

#import "Item.h"

@implementation Item

-(instancetype)initWithString:(NSString *)desStr andimgPath:(NSString *)imgPath{
 self = [super init];
 if (self) {
  self.desStr = desStr;
  self.imgPath = imgPath;
 }
 return self;
}

@end

2、然後創建一個Model類用於封裝自定義的圖標模型,我的模型是將Model類繼承於UIScrollView類,然後設置其可以滾動,然後再創建一個佔據整個scrollview可滾動部分大小的button添加上去。再分別在button上半部分添加UIImageView顯示圖片,在下半部分添加UILabel顯示描述文字,結構如下

重寫model的init方法,在創建對象時用item對象初始化:
model類:

1).h文件

#import <UIKit/UIKit.h>
#import "Item.h"

@interface Model : UIScrollView

@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)UILabel *label;
//判斷button是否被點擊
@property(nonatomic,assign)BOOL isClicked;


-(instancetype)initWithItem:(Item *)item;

//重置模型
-(void)resetModel;

@end

2).m文件

-(instancetype)initWithItem:(Item *)item{
 self = [super initWithFrame:CGRectMake(20, 20, 80, 100)];
 if (self) {
  //設置模塊
  self.contentSize = CGSizeMake(80, self.frame.size.height * 2);
  self.pagingEnabled = NO;
  
  
  //設置模塊屬性
  self.button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.contentSize.height)];
  [self.button addTarget:self action:@selector(buttonDidClicked) forControlEvents:UIControlEventTouchUpInside];
  //添加圖片視圖到button上
  UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  [imgView setImage:[UIImage imageNamed:item.imgPath]];
  [self.button addSubview:imgView];
  //設置button是否被點擊
  self.isClicked = NO;
  [self addSubview:self.button];
  
  self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height)];
  self.label.text = item.desStr;
  self.label.font = [UIFont systemFontOfSize:15];
  self.label.textColor = [UIColor blackColor];
  self.label.numberOfLines = 0;
  self.label.textAlignment = NSTextAlignmentLeft;
  [self addSubview:self.label];
 }
 return self;
}

3)button的點擊事件:即點擊圖片文字描述就會從下面升上來,再點擊就會降下去的動作:

/label升降
-(void)buttonDidClicked{
 if (self.isClicked == NO) {
  [UIView animateWithDuration:0.5 animations:^{
   self.contentOffset = CGPointMake(0, self.frame.size.height);
  }];
  self.isClicked = YES;
 }else if (self.isClicked == YES) {
  [UIView animateWithDuration:0.5 animations:^{
   self.contentOffset = CGPointMake(0, 0);
  }];
  self.isClicked = NO;
 }
}

另外,由於必須保證每次添加model到視圖上時顯示的是圖片,所以需要一個方法來複原到初始狀態,即一旦從視圖上刪除就復原:

//復原
-(void)resetModel{
  self.contentOffset = CGPointMake(0, 0);
  self.isClicked = NO;
}

3、模型準備好了,下面在viewController類裏面寫一個方法將plist文件數據讀取出來封裝到item對象裏面,再用item對象初始化model對象,將所有model對象存入可變數組(_allItems)裏面:

//加載數據到物品
-(void)loadData{
//讀取數據
 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"];
 NSArray *itemArr = [NSArray arrayWithContentsOfFile:filePath];
 //創建模型
 for (int i =0;i <[itemArr count] ; i++) {
  Item *item = [[Item alloc] initWithString:[[itemArr objectAtIndex:i] objectForKey:@"title"] andimgPath:[[itemArr objectAtIndex:i] objectForKey:@"pic"]];
  Model *model = [[Model alloc] initWithItem:item];
  //未被添加的爲0,添加好的爲1
  model.tag = 0;
  [_allItems addObject:model];
 }
}

**注意:**model的tag是用於判斷model是否已經被添加到視圖裏面,從而只會添加數組裏面未添加的model,已添加的model也會用一個數組(displayedItems)來存儲,方便刪除

4、添加和刪除按鈕及其響應的方法:

1)add按鈕:

創建:

//添加添加按鈕
 UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(_width*2/3, _height/10, 40, 40)];
 [addButton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
 [addButton addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:addButton];

add方法:

//添加事件
-(void)add{
 NSInteger itemCount = [_displayedItems count];
 for (Model* model in _allItems) {
  if (model.tag == 0) {
   switch (itemCount) {
    case 1:
     model.frame = CGRectMake(40 + model.frame.size.width, 20, 80, 100);
     break;
    case 2:
     model.frame = CGRectMake(60 + model.frame.size.width*2, 20, 80, 100);
     break;
    case 3:
     model.frame = CGRectMake(20,40 + model.frame.size.height, 80, 100);
     break;
    case 4:
     model.frame = CGRectMake(40 + model.frame.size.width, 40 + model.frame.size.height, 80, 100);
     break;
    case 5:
     model.frame = CGRectMake(60 + model.frame.size.width*2, 40 + model.frame.size.height, 80, 100);
     break;
    default:
     break;
   }
   [_scrollView addSubview:model];
   [_displayedItems addObject:model];
   model.tag = 1;
   break;
  }
 }
}

2)delete按鈕:

//添加刪除按鈕
 UIButton *deleteButton = [[UIButton alloc] initWithFrame: CGRectMake(_width/5, _height/10, 40, 40)];
 [deleteButton setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
 [deleteButton addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:deleteButton];

delete方法:

-(void)delete{
 Model *model = _displayedItems.lastObject;
 [model removeFromSuperview];
 model.tag = 0;
 [model resetModel];
 [_displayedItems removeObject:model];
}

嗯,由於這裏爲了方便,所以添加控件時的位置判斷直接寫死了,所以還有待改進。以上就是用按鈕添加控件這個demo的主要部分,另外還有那個背景圖片的模糊處理使用的是UIVisualEffectView類實現的,在此不詳述了。

代碼不足之處:

1、位置判斷寫死了
2、模型其實建一個類就夠了,Item類有點多餘

進階方案:

1、通過拖動圖標放置在父視圖任何位置
2、點擊控件文字顯示於圖片之上,圖片成爲背景並虛化

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章