Custom Sort button

Source: Internet
Author: User

Now a lot of apps, especially shopping, have filtered sort, like ascending descending kind, recent projects have done a similar share to everyone.

1. Prepare two photos

2. Defining the UIButton Subclass

#import <UIKit/UIKit.h>typedef  void(^dropbtnclickblock) (BOOL isseelect) ; @interface  *title; @property (Nonatomic,strong) uiimageview  *upordownimg; @property (nonatomic,copy)   Dropbtnclickblock Btnclickblock; @end
#import "DropBtn.h"#defineRGB (r,g,b) [Uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]@implementationdropbtn-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {_title=[[NSString Alloc]init]; _upordownimg=[[Uiimageview alloc]init]; _upordownimg.image=[uiimage imagenamed:@"Yzp_data_dropdown.png"]; _upordownimg.frame=cgrectmake (self.frame.size.width- --8, (frame.size.height-5)/2,8,5);        [Self addsubview:_upordownimg];            [Self addtarget:self Action: @selector (Btnclickhandle:) forcontrolevents:uicontroleventtouchupinside]; }    returnSelf ;}-(void) DrawRect: (CGRect) rect{[Super Drawrect:rect]; //frame that calculates the stringNsdictionary *[email protected]{nsfontattributename: [Uifont systemfontofsize: -. F],NSFOREGROUNDCOLORATTRIBUTENAME:RGB ( -, -, -)}; CGRect Frame=[_title boundingrectwithsize:cgsizemake (Maxfloat, maxfloat) options:nsstringdrawinguseslinefragmentorigin    Attributes:attributes Context:nil]; [_title drawatpoint:cgpointmake (Self.bounds.size.width-8-frame.size.width- -)/2, (Self.bounds.size.height-frame.size.height)/2) withattributes:attributes];}-(void) Btnclickhandle: (ID) sender{UIButton*btn= (UIButton *) sender; Btn.selected=!btn.selected; _btnclickblock (btn.selected);}-(void) setselected: (BOOL) selected{[Super setselected:selected]; if(!selected) {_upordownimg.image=[uiimage imagenamed:@"Yzp_data_dropdown.png"]; }    Else{_upordownimg.image=[uiimage imagenamed:@"Yzp_data_dropback.png"]; }    }@end

3. Call

DROPBTN *dropbtn=[[dropbtn Alloc]initwithframe:cgrectmake (0, self.view.bounds.size.width/  2)];    Dropbtn.title=@ " price ";    Dropbtn.backgroundcolor=[Uicolor yellowcolor];    Dropbtn.btnclickblock=^(BOOL isSelected)    {            };    [Self.view addsubview:dropbtn];

4. Effects

Custom Sort button

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.