Sometimes we need to use Actionsheet to show, but often the interface of the system is ugly or does not meet the requirements of the UI, so here is a custom, convenient to use later, there is time to write the development of Swift.
The key point of the custom Actionsheet is the UI style modification and design adjustment, as well as the subsequent operation when clicking the cell, and the smoothness of the interface display.
First interface design:
Create a translucent background view;
Then a table, the table is divided into two areas, set the title header, area tail and cell corner
Background-(uiview*) Maskview {if (!_maskview) {_maskview = [[UIView] Alloc]initwithframe:[uiscreen Mainscreen].bo
Unds];
_maskview.backgroundcolor = [Uicolor blackcolor];
_maskview.alpha = 0.5;
_maskview.userinteractionenabled = YES;
return _maskview; }//Table-(UITableView *) TableView {if (!_tableview) {_tableview = [[UITableView Alloc]initwithframe:cgrectze
Ro Style:uitableviewstyleplain];
_tableview.delegate = self;
_tableview.datasource = self;
_tableview.layer.cornerradius = 10;
_tableview.clipstobounds = YES;
_tableview.rowheight = 44.0;
_tableview.bounces = NO;
_tableview.backgroundcolor = [Uicolor Clearcolor];
_tableview.tableheaderview = Self.headview;
_tableview.separatorinset = Uiedgeinsetsmake (0,-50, 0, 0);
[_tableview Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "Onecell"];
return _tableview; #pragma Mark <uiTableviewdelegate,uitableviewdatasource>-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {
return 2; }-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return (section = = 0)? _ce
Llarray.count:1; }-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {UITableView
Cell *cell = [TableView dequeuereusablecellwithidentifier:@ "Onecell"];
if (indexpath.section = = 0) {cell.textLabel.text = _cellarray[indexpath.row];
if (Indexpath.row = = _cellarray.count-1) {//Add Bezier curve, Uibezierpath and cashapelayer design corner style
/* Byroundingcorners is to set the required processing corner parameters, with the following enumerator to choose: TypeDef ns_options (Nsuinteger, Uirectcorner) {
Uirectcornertopleft = 1 << 0,//left upper Corner uirectcornertopright = 1 << 1,//upper right rounded corner Uirectcornerbottomleft = 1 << 2,//left lower rounded corner uirectcornerBottomRight = 1 << 3,//right corner uirectcornerallcorners = ~0ul//corner rounded}; * * Uibezierpath *maskpath = [Uibezierpath bezierpathwithroundedrect:cgrectmake (0, 0, screen_width-(Space_Lin E * 2), tableview.rowheight) byroundingcorners:uirectcornerbottomleft|
Uirectcornerbottomright Cornerradii:cgsizemake (10, 10)];
Cashapelayer *masklayer = [[Cashapelayer alloc]init];
Masklayer.frame = Cell.contentView.bounds;
Masklayer.path = Maskpath.cgpath;
Cell.layer.mask = Masklayer;
} else {cell.textLabel.text = _canceltitle;
Cell.layer.cornerRadius = 10;
} cell.textLabel.textAlignment = Nstextalignmentcenter;
Cell.selectionstyle = Uitableviewcellselectionstylenone;
return cell; }-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {if (indexpath.section = = 0) {if (Self.selectedblock) {self. Selectedblock (Indexpath.row);
} else {if (self.cancelblock) {self.cancelblock ();
} [self dismiss]; }-(CGFloat) TableView: (UITableView *) TableView heightforfooterinsection: (nsinteger) section {return space_line;}-( uiview*) TableView: (UITableView *) TableView viewforfooterinsection: (nsinteger) Section {UIView *footerview = [[UIView A
Lloc]initwithframe:cgrectmake (0, 0, TableView.bounds.size.width, Space_line)];
Footerview.backgroundcolor = [Uicolor Clearcolor];
return footerview; }
The interface design completes, needs to consider is pops up, disappears the question
/Sliding eject
-(void) show {
_tableview.frame = CGRectMake (Space_line, Screen_height, Screen_width-(Space_line * 2), _t Ableview.rowheight * (_cellarray.count + 1) + _headview.bounds.size.height + (Space_line * 2));
[UIView animatewithduration:.5 animations:^{
cgrect rect = _tableview.frame;
RECT.ORIGIN.Y-= _tableview.bounds.size.height;
_tableview.frame = rect;
}];
Sliding disappears
-(void) dismiss {
[UIView animatewithduration:.5 animations:^{
cgrect rect = _tableview.frame;
RECT.ORIGIN.Y + = _tableview.bounds.size.height;
_tableview.frame = rect;
} completion:^ (BOOL finished) {
[self removefromsuperview];}
];
}
#pragma mark------to touch the screen other position
-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *) Event {
[self dismiss];
}
The last is a call to a custom view:
Pop-up Actionsheet
__weak typeof (self) weakself = self;
Jasonactionsheetview *jasonsheetview = [[Jasonactionsheetview alloc]initwithheadview:self.headview CellArray: Self.dataarr canceltitle:@ "Cancel" selectedblock:^ (Nsinteger index) {
//Click cell Follow up action
if (index = = 0) {
WeakSelf.view.backgroundColor = [Uicolor redcolor];
} else if (index = = 1) {
WeakSelf.view.backgroundColor = [Uicolor yellowcolor];
} else{
weakSelf.view.backgroundColor = [Uicolor lightgraycolor];
}
cancelblock:^{
NSLog (@ "clicked Cancel ........");
}];
[Self.view Addsubview:jasonsheetview];
Effect Chart:
Source: Https://github.com/hbblzjy/OC-JasonActionSheet