1. 程式人生 > >IOS開發UI篇--一個支持圖文混排的ActionSheet

IOS開發UI篇--一個支持圖文混排的ActionSheet

log one 簡單介紹 button action 曾經 hot uitable ios

一、簡單介紹

UIActionSheet是IOS提供給我們開發人員的底部彈出菜單控件。一般用於菜單選擇、操作確認、刪除確認等功能。
IOS官方提供的下面方式對UIActionView進行實例化:

- (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
);

從這個api我們能夠看出,我們僅僅能設置文本標題。包含destructiveButtonTitle、cancelButtonTitle和otherButtonTitles,官方提供的該控件並不支持圖文混排。

但有的時候,交互提給我們的需求又須要我們的ActionSheet具有圖文混排的效果。那就須要我們自己仿造系統自帶的ActionSheet,完畢該需求。


項目演演示樣例如以下:

技術分享

二、使用說明

第一步、構建數據模型

@interface Item : NSObject
@property (nonatomic , strong) NSString *icon;//圖片地址
@property (nonatomic , strong) NSString *title;//標題 @end

第二步、依據數據模型構建數據

Item *item1 = [[Item alloc] init];
item1.icon = @"journey_phone";
item1.title = @"15195905888";
Item *item2 = [[Item alloc] init];
item2.icon = @"journey_phone";
item2.title = @"15195905777";
Item *item3 = [[Item alloc] init];
item3.icon = @"journey_phone"
; item3.title = @"15195905777"; NSArray *listData = [NSArray arrayWithObjects:item1,item2,item3, nil];

第三步、使用以上數據將控件初始化

PicAndTextActionSheet *sheet = [[PicAndTextActionSheet alloc] initWithList:listData title:@"撥打電話"];
sheet.delegate = self;//該控件使用的代理模式
[sheet showInView:self];

由於該控件使用了代理模式。所以在當前Controller須要實現下面代理方法:

-(void) didSelectIndex:(NSInteger)index{
}

該代理方法,主要是在Controller中能夠實如今自己定義ActionSheet中的點擊事件。

三、實現原理

由於ActionSheet不能支持圖片的顯示,所以我們就放棄使用擴展UIActionSheet控件的方法。我在本項目中使用的是UITableView+動畫。高仿ActionSheet的方法。

UTableView能夠制作列表選項,動畫能夠實現系統自帶ActionSheet的自底向上和漸變效果。

註意點:

假設tableview處於uiview上面,uiview整個背景有點擊事件。可是我們須要假設我們點擊tableview的時候,處理tableview的點擊事件,而不是uiview的事件。

在這裏。我們須要推斷我們點擊事件是否在uiview上還是在uitableview上。

解決方式例如以下:

1、實現代理:

<UIGestureRecognizerDelegate>

2、讓gesture設置為代理

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
tapGesture.delegate = self;

3、實現代理方法。推斷點擊的是否是uiview還是tableview

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if([touch.view isKindOfClass:[self class]]){
        return YES;
    }
    return NO;
}

四、總結

不論什麽一個復雜控件,基本上都是有基礎控件組合實現而成。該擴展的ActionSheet也能夠用於下面場景:
技術分享



技術分享

五、下載地址

github下載地址:https://github.com/yixiangboy/ActionSheetExtension

假設認為對你還有些用,給一顆star吧。你的支持是我繼續的動力。

blog地址:http://blog.csdn.net/yixiangboy/article/details/46778417

博主的話

曾經看過非常多別人的博客,學到不少東西。如今準備自己也開始寫寫博客,希望能夠幫到一些人。


我的聯系方式:
微博:

topnav=1&wvr=6">新浪微博
博客:http://blog.csdn.net/yixiangboy
github:https://github.com/yixiangboy

IOS開發UI篇--一個支持圖文混排的ActionSheet