iOS Development note (SWIFT)-some modifications to the Ppiflatsegmentedcontrol project for Swift calls

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.