這篇文章主要介紹iOS如何實(shí)現(xiàn)通過按鈕添加和刪除控件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、南和網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、商城系統(tǒng)網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為南和等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。先講一下這個(gè)demo主要部分,即通過按鈕實(shí)現(xiàn)增刪圖標(biāo)
分析:
1、每一個(gè)圖標(biāo)需要兩個(gè)數(shù)據(jù),即圖片和描述用的字符串 ,所以創(chuàng)建一個(gè)Item類來封裝從plist文件讀取出來的數(shù)據(jù):
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、然后創(chuàng)建一個(gè)Model類用于封裝自定義的圖標(biāo)模型,我的模型是將Model類繼承于UIScrollView類,然后設(shè)置其可以滾動,然后再創(chuàng)建一個(gè)占據(jù)整個(gè)scrollview可滾動部分大小的button添加上去。再分別在button上半部分添加UIImageView顯示圖片,在下半部分添加UILabel顯示描述文字,結(jié)構(gòu)如下
重寫model的init方法,在創(chuàng)建對象時(shí)用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是否被點(diǎn)擊@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) { //設(shè)置模塊 self.contentSize = CGSizeMake(80, self.frame.size.height * 2); self.pagingEnabled = NO; //設(shè)置模塊屬性 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]; //設(shè)置button是否被點(diǎn)擊 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的點(diǎn)擊事件:即點(diǎn)擊圖片文字描述就會從下面升上來,再點(diǎn)擊就會降下去的動作:
/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到視圖上時(shí)顯示的是圖片,所以需要一個(gè)方法來復(fù)原到初始狀態(tài),即一旦從視圖上刪除就復(fù)原:
//復(fù)原-(void)resetModel{ self.contentOffset = CGPointMake(0, 0); self.isClicked = NO;}
3、模型準(zhǔn)備好了,下面在viewController類里面寫一個(gè)方法將plist文件數(shù)據(jù)讀取出來封裝到item對象里面,再用item對象初始化model對象,將所有model對象存入可變數(shù)組(_allItems)里面:
//加載數(shù)據(jù)到物品-(void)loadData{//讀取數(shù)據(jù) NSString *filePath = [[NSBundle mainBundle] pathForResource:@"shop" ofType:@"plist"]; NSArray *itemArr = [NSArray arrayWithContentsOfFile:filePath]; //創(chuàng)建模型 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是否已經(jīng)被添加到視圖里面,從而只會添加數(shù)組里面未添加的model,已添加的model也會用一個(gè)數(shù)組(displayedItems)來存儲,方便刪除
4、添加和刪除按鈕及其響應(yīng)的方法:
1)add按鈕:
創(chuàng)建:
//添加添加按鈕 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];}
嗯,由于這里為了方便,所以添加控件時(shí)的位置判斷直接寫死了,所以還有待改進(jìn)。以上就是用按鈕添加控件這個(gè)demo的主要部分,另外還有那個(gè)背景圖片的模糊處理使用的是UIVisualEffectView類實(shí)現(xiàn)的,在此不詳述了。
代碼不足之處:
1、位置判斷寫死了2、模型其實(shí)建一個(gè)類就夠了,Item類有點(diǎn)多余
進(jìn)階方案:
1、通過拖動圖標(biāo)放置在父視圖任何位置2、點(diǎn)擊控件文字顯示于圖片之上,圖片成為背景并虛化
以上是“iOS如何實(shí)現(xiàn)通過按鈕添加和刪除控件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:iOS如何實(shí)現(xiàn)通過按鈕添加和刪除控件-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://jinyejixie.com/article28/ccsgjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、定制開發(fā)、靜態(tài)網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容