The Ppiflatsegmentedcontrol project is a popular open source iOS control library that provides a flat style (Flat style) Segmentedcontrol that can be customized segment colors, icons, sizes, etc. Very flexible and beautiful.
The effect is as follows:
However, as a OC project, when we were programming for OC and Swift, there were some problems due to the use of block in Ppiflatsegmentedcontrol's creation instance method.
Ideas:
First of all, according to the official documentation, Swift uses a closed package instead of block, but I have a limited level and the program does not work properly when used with closures. At the same time, @selector in Swift was verified to work, so I used @selector instead of block.
Implementation scenarios:
1. Modify/Add the instantiation method in Ppiflatsegmentedcontrol source code
here, for Ppiflatsegmentedcontrol Adds a new approach, with the biggest change being the use of @selector.
-(ID) initWithFrame: (CGRect) frame items: (nsarray*) Items iconposition: (iconposition) position target: ( ID) Target andselection: (SEL) Action;
The target and Selaction properties are added to the Ppiflatsegmentedcontrol object as required.
ID target;
The last is to modify the project return Selectindex method segmentselected:, here are two scenarios can be used, respectively, Objc_msgsend () and [ID Performselector: withobject:].
-(void) Segmentselected: (ID) sender{if(sender) {Nsuinteger SelectedIndex=[Self.segments Indexofobject:sender]; [Self setenabled:yes forsegmentatindex:selectedindex]; //calling Block if(Self.selblock) {self.selblock (selectedindex); } if(_selaction!=Nil) { //objc_msgsend (_target,_selaction,[nsnumber numberwithinteger:selectedindex],selectedindex);[_target performselector:_selaction withobject:[nsnumber Numberwithint:selectedindex]; } }}
Here, we have modified the OC source code is complete, following the swift call to the demo.
var data=[["text":"test1"],["text":"test2"]]; var Segmentcontrol= Ppiflatsegmentedcontrol (Frame:cgrectmake (yunshouyi.screen_width/4,Ten, yunshouyi.screen_width/2, -), Items:data, Iconposition:iconpositionright, Target:self, andselection:"segmentcontrolselected:") Segmentcontrol.color=textservcie.getcolorfromhex ("#36b5fc") Segmentcontrol.borderwidth=0Segmentcontrol.selectedcolor=textservcie.getcolorfromhex ("#0193e6") Segmentcontrol.selectedtextattributes=[nsfontattributename:uifont.systemfontofsize ( -), NSForegroundColorAttributeName:UIColor.whiteColor ()] Segmentcontrol.textattributes=[nsfontattributename:uifont.systemfontofsize ( -), NSForegroundColorAttributeName:TextServcie.getcolorfromHEX ("#0971b0")] Self.navigationItem.titleView=segmentcontrol;
Call the @selector code:
func segmentcontrolselected (index:nsnumber) { if(index.intvalue==0) { Switchtofoundation () }else{ switchtop2p () } }
Implementation results:
Ppiflatsegmentedcontrol Project Address: Https://github.com/pepibumur/PPiFlatSegmentedControl
iOS Development note (SWIFT)-some modifications to the Ppiflatsegmentedcontrol project for Swift calls