actionSheet+Button(按钮)

来源:互联网 发布:php框架开发视频教程 编辑:程序博客网 时间:2024/06/05 18:35

用custometype方式创建按钮,不用背景图片,就可以设置按钮按下时的背景色。按下时按钮时背景是圆角的。要设置这个属性masktobounds为yes.

////  UCActionSheet.h//  Zandouji////  Created by laizhenjie on 14-3-28.//  Copyright (c) 2014年 Laizhenjie. All rights reserved.//#import <UIKit/UIKit.h>@interface UCActionSheet : UIView@property(nonatomic, strong) UIButton *favorButton;@property(nonatomic, strong) UIButton *cancelButton;@property(nonatomic, strong) UIButton *reportButton;@property(nonatomic, retain) UIView *bgViewForBts;@property(nonatomic, retain) UIView *bgColorAlpaView;- (void)show;@end

////  UCActionSheet.m//  Zandouji////  Created by laizhenjie on 14-3-28.//  Copyright (c) 2014年 Laizhenjie. All rights reserved.//#import "UCActionSheet.h"#import "UCColor+HexString.h"@interface UCActionSheet ()@end@implementation UCActionSheet@synthesize reportButton;@synthesize cancelButton;@synthesize favorButton;@synthesize bgViewForBts;@synthesize bgColorAlpaView;@synthesize amuseBean;@synthesize identifiId;//初始化函数- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {                CGRect rect = [UIScreen mainScreen].bounds;        self.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);        self.backgroundColor = [UIColor clearColor];        [self initBgViewWithFrame:rect];    }        return self;}#define KBG_VIEW_WIDTH 320#define KBG_VIEW_HEIGHT 120- (void)initBgViewWithFrame:(CGRect)frame{        bgColorAlpaView = [[UIView alloc]initWithFrame:frame];    bgColorAlpaView.backgroundColor = [UIColor blackColor];    bgColorAlpaView.alpha = 0.5;    [self addSubview:bgColorAlpaView];        bgViewForBts = [[UIView alloc]initWithFrame:CGRectMake( frame.size.width-KBG_VIEW_WIDTH, frame.size.height-KBG_VIEW_HEIGHT, KBG_VIEW_WIDTH, KBG_VIEW_WIDTH)];    [self addSubview:bgViewForBts];    [bgViewForBts setBackgroundColor:[UIColor clearColor]];    bgViewForBts.alpha = 1.0f;        [self initWithSubviews:frame];        }#define KBUTTON_WHDTH  280#define KBUTTON_HEIGHT 44#define KVERTICAL_INTERVAL 5-(void)initWithSubviews:(CGRect)frame{        CGFloat dynamicY = 5.0f;    favorButton = [[UIButton alloc]initWithFrame:CGRectMake(20.f, 5.0, KBUTTON_WHDTH, KBUTTON_HEIGHT)];    [favorButton addTarget:self action:@selector(clickedFavorBtn:) forControlEvents:UIControlEventTouchUpInside];    [favorButton setTitle:@"收藏" forState:UIControlStateNormal];    [favorButton setTitleColor:[UCColor_HexString colorWithHexString:@"#68c49d"] forState:UIControlStateNormal];    [favorButton setTag:1];    [self setButtonAppearence:favorButton];    [bgViewForBts addSubview:favorButton];                dynamicY = favorButton.frame.size.height +favorButton.frame.origin.y+5.0;    reportButton = [[UIButton alloc]initWithFrame:CGRectMake(20.f, dynamicY, KBUTTON_WHDTH, KBUTTON_HEIGHT)];    [reportButton addTarget:self action:@selector(clickedReportBtn:) forControlEvents:UIControlEventTouchUpInside];    [reportButton setTag:2];    [reportButton setTitle:@"举报" forState:UIControlStateNormal];    [reportButton setTitleColor:[UCColor_HexString colorWithHexString:@"#d15050"] forState:UIControlStateNormal];    [self setButtonAppearence:reportButton];    [bgViewForBts addSubview:reportButton];    dynamicY = reportButton.frame.size.height +reportButton.frame.origin.y+5.0;    cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(20.f, dynamicY, KBUTTON_WHDTH, KBUTTON_HEIGHT)];    [cancelButton addTarget:self action:@selector(clickedCancelBtn:) forControlEvents:UIControlEventTouchUpInside];    [cancelButton setTag:3];    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];    [cancelButton setTitleColor:[UCColor_HexString colorWithHexString:@"#666666"] forState:UIControlStateNormal];    [self setButtonAppearence:cancelButton];    [bgViewForBts addSubview:cancelButton];        bgViewForBts.frame = CGRectMake( frame.size.width-KBG_VIEW_WIDTH, frame.size.height-(KBUTTON_HEIGHT + KVERTICAL_INTERVAL)*3.0-KVERTICAL_INTERVAL*3.0, KBG_VIEW_WIDTH, (KBUTTON_HEIGHT + KVERTICAL_INTERVAL)* 3.0+KVERTICAL_INTERVAL*3.0);    }#pragma mark -设置按钮的外观- (void)setButtonAppearence:(UIButton *)button{    button.layer.cornerRadius = 4.0f;    button.layer.masksToBounds = YES;    button.backgroundColor = [UCColor_HexString colorWithHexString:@"#ffffff"];    [button setBackgroundImage:[self buttonImageFromColor:[UCColor_HexString colorWithHexString:@"#dcdada"]] forState:UIControlStateHighlighted];//    button.showsTouchWhenHighlighted = YES;  //发光效果}//画一个view转成image- (UIImage *) buttonImageFromColor:(UIColor *)color {    CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();    CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return img;}#pragma mark -- (void)show{            [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self];    [self addGestureSetting];    }- (IBAction)clickedFavorBtn:(UIButton *)sender{    NSLog(@"clickedFavorBtn.");    [self removeFromSuperview];    }- (IBAction)clickedReportBtn:(UIButton *)sender{       [self removeFromSuperview];}- (IBAction)clickedCancelBtn:(UIButton *)sender{        [self removeFromSuperview];}#pragma mark - 添加单击、双击事件;-(void)addGestureSetting{        UITapGestureRecognizer *singleTapGestureInView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGestureInViewEvent:)];    singleTapGestureInView.numberOfTapsRequired = 1; //单击一下;    [self addGestureRecognizer:singleTapGestureInView];    [singleTapGestureInView release];    }#pragma mark - 单击/双击事件函数;//单击事件关联函数:关闭当前的页面;-(IBAction)singleTapGestureInViewEvent:(UITapGestureRecognizer *)sender{        [self removeFromSuperview];    }#pragma mark 释放函数- (void)dealloc{        [super dealloc];    }@end

效果:


0 0
原创粉丝点击