码迷,mamicode.com
首页 > 编程语言 > 详细

自定义排序按钮

时间:2015-10-05 19:25:51      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

现在好多APP特别是购物的有筛选排序, 像升序降序那种,最近项目做了个类似的分享给大家。

1.准备两个图片

技术分享技术分享

2.定义UIButton子类

#import <UIKit/UIKit.h>

typedef  void(^DropBtnClickBlock)(BOOL isSeelect);

@interface DropBtn : UIButton

@property (nonatomic,strong) NSString *title;

@property (nonatomic,strong) UIImageView  *upOrDownImg;

@property (nonatomic,copy)   DropBtnClickBlock btnClickBlock;
@end
#import "DropBtn.h"
#define RGB(r,g,b)                  [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

@implementation DropBtn

- (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-20-8, (frame.size.height-5)/2, 8, 5);
        [self addSubview:_upOrDownImg];
        [self addTarget:self action:@selector(btnClickHandle:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}
-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    //计算出字符串的frame
    NSDictionary *attributes=@{NSFontAttributeName: [UIFont systemFontOfSize:20.f],NSForegroundColorAttributeName:RGB(26, 26, 26)};
    CGRect frame=[_title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    [_title drawAtPoint:CGPointMake((self.bounds.size.width-8-frame.size.width-20)/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.调用

 DropBtn *dropBtn=[[DropBtn alloc]initWithFrame:CGRectMake(0, 44, self.view.bounds.size.width/2, 60)];
    dropBtn.title=@"价格";
    dropBtn.backgroundColor=[UIColor yellowColor];
    dropBtn.btnClickBlock=^(BOOL isSelected)
    {
        
    };
    [self.view addSubview:dropBtn];

4.效果

技术分享技术分享

自定义排序按钮

标签:

原文地址:http://www.cnblogs.com/5ishare/p/4856051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!