定制二选一按钮SwitchButton

定制二选一按钮SwitchButton

效果:

源码:

SwitchButton.h 与 SwitchButton.m

//
//  SwitchButton.h
//  KongJian
//
//  Created by YouXianMing on 14/10/24.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol SwitchButtonDelegate <NSObject>
- (void)switchButtonState:(BOOL)state;
@end

@interface SwitchButton : UIView

/**
 *  代理
 */
@property (nonatomic, assign) id<SwitchButtonDelegate> delegate;

/**
 *  3个view
 */
@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UIView *rightView;
@property (nonatomic, strong) UIView *blockView;

/**
 *  动画持续时间
 */
@property (nonatomic, assign) NSTimeInterval duration;

/**
 *  块是否在左边
 */
@property (nonatomic, assign) BOOL    blockAtLeft;

/**
 *  背景颜色
 */
@property (nonatomic, strong) UIColor  *leftBackgroundColor;
@property (nonatomic, strong) UIColor  *rightBackgroundColor;

/**
 *  便利的创建出按钮
 *
 *  @param leftText  左边显示的文本
 *  @param rightText 右边显示的文本
 *  @param size      button的尺寸
 *
 *  @return button对象
 */
+ (SwitchButton *)createDefaultTypeButtonWithLeftText:(NSString *)leftText
                                            rightText:(NSString *)rightText
                                                 size:(CGSize)size;

@end
//
//  SwitchButton.m
//  KongJian
//
//  Created by YouXianMing on 14/10/24.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "SwitchButton.h"

@interface SwitchButton ()

@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) UIView   *leftBackView;
@property (nonatomic, strong) UIView   *rightBackView;
@property (nonatomic, strong) UIView   *blockBackView;

@end

@implementation SwitchButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;

        // 按钮
        _button = [[UIButton alloc] initWithFrame:self.bounds];
        [_button addTarget:self
                    action:@selector(buttonEvent)
          forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_button];

        // 左侧view
        _leftBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
                                                                 self.bounds.size.width / 2.f,
                                                                 self.bounds.size.height)];
        _leftBackView.userInteractionEnabled = NO;
        [self addSubview:_leftBackView];

        // 右侧view
        _rightBackView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2.f, 0,
                                                                  self.bounds.size.width / 2.f, self.bounds.size.height)];
        _rightBackView.userInteractionEnabled = NO;
        [self addSubview:_rightBackView];

        // 遮挡用的view
        _blockBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
                                                                  self.bounds.size.width / 2.f,
                                                                  self.bounds.size.height)];
        _blockBackView.userInteractionEnabled = NO;
        [self addSubview:_blockBackView];

    }
    return self;
}

- (void)buttonEvent {
    if (_blockBackView.frame.origin.x == 0) {
        if (_delegate) {
            [_delegate switchButtonState:YES];
        }

        [UIView animateWithDuration:(_duration > 0 ? _duration : 0.2f)
                         animations:^{
                             _blockBackView.frame = CGRectMake(self.bounds.size.width / 2.f, 0,
                                                               self.bounds.size.width / 2.f, self.bounds.size.height);
                             if (_leftBackgroundColor) {
                                 _button.backgroundColor = _leftBackgroundColor;
                             }

                         } completion:^(BOOL finished) {

                         }];
    } else {
        [_delegate switchButtonState:NO];
        [UIView animateWithDuration:(_duration > 0 ? _duration : 0.2f)
                         animations:^{
                             _blockBackView.frame = CGRectMake(0, 0,
                                                               self.bounds.size.width / 2.f, self.bounds.size.height);
                             if (_rightBackgroundColor) {
                                 _button.backgroundColor = _rightBackgroundColor;
                             }
                         } completion:^(BOOL finished) {

                         }];
    }
}

@synthesize leftView = _leftView;
- (void)setLeftView:(UIView *)leftView {
    _leftView = leftView;
    leftView.userInteractionEnabled = NO;
    [_leftBackView addSubview:leftView];

    [self bringSubviewToFront:_blockBackView];
}

@synthesize rightView = _rightView;
- (void)setRightView:(UIView *)rightView {
    _rightView = rightView;
    rightView.userInteractionEnabled = NO;
    [_rightBackView addSubview:rightView];

    [self bringSubviewToFront:_blockBackView];
}

@synthesize blockView = _blockView;
- (void)setBlockView:(UIView *)blockView {
    _blockView = blockView;
    blockView.userInteractionEnabled = NO;
    [_blockBackView addSubview:blockView];

    [self bringSubviewToFront:_blockBackView];
}

@synthesize blockAtLeft = _blockAtLeft;
- (void)setBlockAtLeft:(BOOL)blockAtLeft {
    _blockAtLeft = blockAtLeft;
    if (blockAtLeft == YES) {
        _blockBackView.frame = CGRectMake(0, 0,
                                          self.bounds.size.width / 2.f,
                                          self.bounds.size.height);
    } else {
        _blockBackView.frame = CGRectMake(self.bounds.size.width / 2.f, 0,
                                          self.bounds.size.width / 2.f, self.bounds.size.height);
    }
}

#pragma mark - 便利构造器
+ (SwitchButton *)createDefaultTypeButtonWithLeftText:(NSString *)leftText
                                            rightText:(NSString *)rightText
                                                 size:(CGSize)size {
    SwitchButton *button        = [[SwitchButton alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    button.layer.cornerRadius   = 7.f;
    button.blockAtLeft          = NO;
    button.leftBackgroundColor  = [UIColor colorWithRed:0.969 green:0.365 blue:0.137 alpha:1];
    button.backgroundColor      = [UIColor colorWithRed:0.969 green:0.365 blue:0.137 alpha:1];
    button.rightBackgroundColor = [UIColor colorWithRed:0.278 green:0.835 blue:0.855 alpha:1];

    button.duration             = 0.3f;

    // 左侧文本
    UILabel *man      = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, button.bounds.size.width/2.f,
                                                                  button.bounds.size.height)];
    man.text          = leftText;
    man.textColor     = [UIColor whiteColor];
    man.font          = [UIFont systemFontOfSize:20.f];
    man.textAlignment = NSTextAlignmentCenter;
    button.leftView   = man;

    // 右侧文本
    UILabel *woman      = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, button.bounds.size.width/2.f,
                                                                    button.bounds.size.height)];
    woman.text          = rightText;
    woman.textColor     = [UIColor whiteColor];
    woman.font          = [UIFont systemFontOfSize:20.f];
    woman.textAlignment = NSTextAlignmentCenter;
    button.rightView    = woman;

    // 中间挡住的块
    UIView *blockView = [[UIView alloc] initWithFrame:CGRectMake(4, 4, button.bounds.size.width/2.f - 8,
                                                                 button.bounds.size.height - 8)];
    blockView.layer.cornerRadius = 7.f;
    blockView.backgroundColor = [UIColor whiteColor];
    button.blockView = blockView;

    return button;
}

@end

使用时候源码:

//
//  ViewController.m
//  KongJian
//
//  Created by YouXianMing on 14/10/24.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "SwitchButton.h"

@interface ViewController ()<SwitchButtonDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    SwitchButton *button = [SwitchButton createDefaultTypeButtonWithLeftText:@"Y"
                                                                   rightText:@"X"
                                                                        size:CGSizeMake(110, 70)];
    button.delegate      = self;
    button.center        = self.view.center;
    [self.view addSubview:button];
}

- (void)switchButtonState:(BOOL)state {
    NSLog(@"%d", state);
}

@end

核心的地方:

便利构造器(你可以自己去写):

时间: 2024-10-30 16:15:55

定制二选一按钮SwitchButton的相关文章

复选框按钮在IE浏览器中出现重叠现象

问题描述 复选框按钮在IE浏览器中出现重叠现象 IE浏览器的复选框按钮出现了样式的重叠,怎么解决 解决方案 按F12,查看一下样式哪里出了问题: 解决方案二: 标签那栏高度太高,把下面的背景图片一起显示出来了吧..用的什么插件也不说 解决方案三: 如果是兼容性的问题,建议升级到最新的浏览器看看,IE速度比较慢,还是Google的浏览器比较好

设计多选一按钮ChooseOnlyButton

设计多选一按钮ChooseOnlyButton 效果: 源码: ChooseOnlyButton.h 与 ChooseOnlyButton.m // // ChooseOnlyButton.h // ChooseOnlyButton // // Created by YouXianMing on 14/11/4. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @class

设计可以多选的按钮ChooseManyButton

设计可以多选的按钮ChooseManyButton 效果: 源码: ChooseManyButton.h 与 ChooseManyButton.m // // ChooseManyButton.h // ChooseOnlyButton // // Created by YouXianMing on 14/11/5. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @prot

话题:如果腾讯游戏和360二选一你还玩吗?

11月3日下午,对于360与QQ之间的纠纷,腾讯发布公告,称在双方纠纷解决之前,腾讯将在装有360软件的电脑上停止运行QQ软件.业内认为,腾讯此举是逼迫用户作出二选一的选择. 从今年9月底开始,中国最大的两家客户端软件公司腾讯和360之间发生了一场持续至今的纠纷,导火索是360发布隐私保护器,称QQ涉嫌扫描网民电脑隐私文件,腾讯则矢口否认:双方最近的一次交锋是360发布可以去QQ广告.关闭QQ秀及QQ会员的"扣扣保镖",腾讯则以不正当竞争为由将360告上法院. 腾讯发出弹窗提示,逼迫用

ionic 多选按钮-ionic 复选框按钮问题

问题描述 ionic 复选框按钮问题 在一个页面有一个多选按钮,选中多个以后 ,跳转到另一个页面,做相应处理后,在跳回 有多选按钮的页面,怎么让选中的选项还存在. 解决方案 http://www.runoob.com/ionic/ionic-ion-radio.html

如何在单击服务器端单选按钮时弹出我所选的按钮的文字,请大家指点一下,

问题描述 如何在单击服务器端单选按钮时弹出我所选的按钮的文字,请大家指点一下,

js实现iPhone界面风格的单选框和复选框按钮实例_javascript技巧

本文实例讲述了js实现iPhone界面风格的单选框和复选框按钮.分享给大家供大家参考.具体如下: 这里使用JS美化仿iPhone风格的单选框和复选框按钮效果,使用了jQuery代码,附有完整实例及使用方法,现在,iPhone风格确实流行,希望大家也喜欢. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-iphone-radio-checkbox-button-codes/ 具体代码如下: <!DOCTYPE html PUBLIC "

公关大神们 收了“二选一”这个神通吧

中介交易 SEO诊断 淘宝客 云主机 技术大厅 是商业竞争,就老老实实应战,出来混就坚强点,别梨花带雨的放一段哭腔,再用蒙混不清似是而非的概念忽悠观众. 伯通(微信公众号:微板报,weibanbao) 别闹.别慌.别吹--"红六月"来袭,在这个由中国B2C电商们新造的网购节日中,最吸引眼球的,除了各家那些似乎来自同一个设计公司的宣战海报外,便是有关"二选一"的指责与回应. "二选一"和"抄袭"一样,是国内互联网公司指责对手的常

遨游声音太大,视频网站反弹 考虑用户二选一

中介交易 SEO诊断 淘宝客 云主机 技术大厅 傲游浏览器推出视频广告快进功能,已引起了视频大佬的警觉并反弹.据遨游创始人Jeff微博透露,遨游已收到中国的几大视频网站巨头的律师罕或公文."且某些视频网站在考虑让用户二选一". 视频网站先礼后兵,如果遨游继续提供快进功能,或将被视频网站封杀,抑或遭遇诉讼--不论结果,诉诸公堂终究是两败俱伤的零和游戏.在动粗之前视频网站将可能会开展舆论攻势以及技术反制措施,这将是让人想起就打瞌睡的无聊战役. 理论上来讲,傲游的浏览器快进功能短期内不会对视