用动画切换按钮的状态

效果

源码

https://github.com/YouXianMing/UI-Component-Collection

//
//  BaseControl.h
//  BaseButton
//
//  Created by YouXianMing on 15/8/27.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>
@class BaseControl;@protocol BaseControlDelegate <NSObject>@optional/***  点击事件触发**  @param control BaseControl对象*/
- (void)baseControlTouchEvent:(BaseControl *)control;@end@interface BaseControl : UIView/***  代理方法*/
@property (nonatomic, weak) id <BaseControlDelegate>  delegate;#pragma mark - 以下方法需要子类重载/***  触发了点击事件*/
- (void)touchEvent;/***  拖拽到rect外面触发的事件*/
- (void)touchDragExit;/***  点击事件开始*/
- (void)touchBegin;@end

//
//  BaseControl.m
//  BaseButton
//
//  Created by YouXianMing on 15/8/27.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "BaseControl.h"@interface BaseControl ()@property (nonatomic, strong) UIButton *button;@end@implementation BaseControl- (instancetype)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {[self baseControlSetup];}return self;
}- (void)baseControlSetup {_button = [[UIButton alloc] initWithFrame:self.bounds];[self addSubview:_button];// 开始点击[_button addTarget:self action:@selector(touchBegin) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];// 拖拽到rect外面[_button addTarget:self action:@selector(touchDragExit) forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel];// 触发事件
    [_button addTarget:self action:@selector(touchEvent) forControlEvents:UIControlEventTouchUpInside];
}- (void)touchEvent {[NSException raise:NSInternalInconsistencyExceptionformat:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",[NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}- (void)touchDragExit {[NSException raise:NSInternalInconsistencyExceptionformat:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",[NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}- (void)touchBegin {[NSException raise:NSInternalInconsistencyExceptionformat:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",[NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}@end

//
//  CustomButton.h
//  CustomButton
//
//  Created by YouXianMing on 16/5/21.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "BaseControl.h"typedef NS_OPTIONS(NSUInteger, BaseControlState) {BaseControlStateNormal = 1000,BaseControlStateHighlighted,BaseControlStateDisabled,
};@interface CustomButton : BaseControl/***  目标*/
@property (nonatomic, weak) id target;/***  按钮事件*/
@property (nonatomic) SEL      buttonEvent;/***  普通背景色*/
@property (nonatomic, strong) UIColor  *normalBackgroundColor;/***  高亮状态背景色*/
@property (nonatomic, strong) UIColor  *highlightBackgroundColor;/***  禁用状态背景色*/
@property (nonatomic, strong) UIColor  *disabledBackgroundColor;/***  状态值*/
@property (nonatomic, readonly) BaseControlState  state;/***  按钮标题*/
@property (nonatomic, strong) NSString *title;/***  字体*/
@property (nonatomic, strong) UIFont   *font;/***  水平位移*/
@property (nonatomic) CGFloat  horizontalOffset;/***  垂直位移*/
@property (nonatomic) CGFloat  verticalOffset;/***  对其方式*/
@property (nonatomic) NSTextAlignment   textAlignment;/***  给标题设置颜色**  @param color 颜色*  @param state 状态*/
- (void)setTitleColor:(UIColor *)color state:(BaseControlState)state;/***  切换到不同的状态**  @param state    状态*  @param animated 是否执行动画*/
- (void)changeToState:(BaseControlState)state animated:(BOOL)animated;@end

//
//  CustomButton.m
//  CustomButton
//
//  Created by YouXianMing on 16/5/21.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "CustomButton.h"@interface CustomButton ()@property (nonatomic) BaseControlState  state;
@property (nonatomic) BOOL              enableEvent;
@property (nonatomic, strong) UILabel  *normalLabel;
@property (nonatomic, strong) UILabel  *highlightedLabel;
@property (nonatomic, strong) UILabel  *disabledLabel;
@property (nonatomic, strong) UIView   *backgroundView;@end@implementation CustomButton- (instancetype)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {// 激活_enableEvent = YES;// 背景viewself.backgroundView                 = [[UIView alloc] initWithFrame:self.bounds];self.backgroundView.backgroundColor = [UIColor clearColor];[self addSubview:self.backgroundView];// Labelself.normalLabel               = [[UILabel alloc] initWithFrame:self.bounds];self.normalLabel.textAlignment = NSTextAlignmentCenter;self.normalLabel.textColor     = [UIColor clearColor];[self addSubview:self.normalLabel];self.highlightedLabel               = [[UILabel alloc] initWithFrame:self.bounds];self.highlightedLabel.textAlignment = NSTextAlignmentCenter;self.highlightedLabel.textColor     = [UIColor clearColor];[self addSubview:self.highlightedLabel];self.disabledLabel               = [[UILabel alloc] initWithFrame:self.bounds];self.disabledLabel.textAlignment = NSTextAlignmentCenter;self.disabledLabel.textColor     = [UIColor clearColor];[self addSubview:self.disabledLabel];// backgroundViewself.backgroundView.userInteractionEnabled   = NO;self.normalLabel.userInteractionEnabled      = NO;self.highlightedLabel.userInteractionEnabled = NO;self.disabledLabel.userInteractionEnabled    = NO;}return self;
}- (void)setTitleColor:(UIColor *)color state:(BaseControlState)state {if (state == BaseControlStateNormal) {self.normalLabel.textColor = color;} else if (state == BaseControlStateHighlighted) {self.highlightedLabel.textColor = color;} else if (state == BaseControlStateDisabled) {self.disabledLabel.textColor = color;}
}#pragma mark - 重载的方法- (void)touchEvent {if (_enableEvent == NO) {return;}[self changeToState:BaseControlStateNormal animated:YES];if (self.delegate && [self.delegate respondsToSelector:@selector(baseControlTouchEvent:)]) {[self.delegate baseControlTouchEvent:self];}if (self.buttonEvent && self.target) {#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"[self.target performSelector:self.buttonEvent withObject:self];
#pragma clang diagnostic pop}
}- (void)touchDragExit {if (_enableEvent == NO) {return;}[self changeToState:BaseControlStateNormal animated:YES];
}- (void)touchBegin {if (_enableEvent == NO) {return;}[self changeToState:BaseControlStateHighlighted animated:YES];
}#pragma mark -- (void)changeToState:(BaseControlState)state animated:(BOOL)animated {_state = state;if (state == BaseControlStateNormal) {_enableEvent = YES;[self normalStateAnimated:animated];} else if (state == BaseControlStateHighlighted) {_enableEvent = YES;[self highlightedAnimated:animated];} else if (state == BaseControlStateDisabled) {_enableEvent = NO;[self disabledAnimated:animated];}
}- (void)normalStateAnimated:(BOOL)animated {if (!animated) {self.normalLabel.alpha      = 1.f;self.highlightedLabel.alpha = 0.f;self.disabledLabel.alpha    = 0.f;self.backgroundView.backgroundColor = self.normalBackgroundColor;} else {[UIView animateWithDuration:0.25f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{self.normalLabel.alpha      = 1.f;self.highlightedLabel.alpha = 0.f;self.disabledLabel.alpha    = 0.f;self.backgroundView.backgroundColor = self.normalBackgroundColor;} completion:nil];}
}- (void)highlightedAnimated:(BOOL)animated {if (!animated) {self.normalLabel.alpha      = 0.f;self.highlightedLabel.alpha = 1.f;self.disabledLabel.alpha    = 0.f;self.backgroundView.backgroundColor = self.highlightBackgroundColor;} else {[UIView animateWithDuration:0.25f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{self.normalLabel.alpha      = 0.f;self.highlightedLabel.alpha = 1.f;self.disabledLabel.alpha    = 0.f;self.backgroundView.backgroundColor = self.highlightBackgroundColor;} completion:nil];}
}- (void)disabledAnimated:(BOOL)animated {if (!animated) {self.normalLabel.alpha      = 0.f;self.highlightedLabel.alpha = 0.f;self.disabledLabel.alpha    = 1.f;self.backgroundView.backgroundColor = self.disabledBackgroundColor;} else {[UIView animateWithDuration:0.25f delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{self.normalLabel.alpha      = 0.f;self.highlightedLabel.alpha = 0.f;self.disabledLabel.alpha    = 1.f;self.backgroundView.backgroundColor = self.disabledBackgroundColor;} completion:nil];}
}#pragma mark - 重写getter,setter方法- (void)setTitle:(NSString *)title {_title = title;self.normalLabel.text      = title;self.highlightedLabel.text = title;self.disabledLabel.text    = title;
}- (void)setTextAlignment:(NSTextAlignment)textAlignment {_textAlignment = textAlignment;self.normalLabel.textAlignment      = textAlignment;self.highlightedLabel.textAlignment = textAlignment;self.disabledLabel.textAlignment    = textAlignment;
}- (void)setFont:(UIFont *)font {_font = font;self.normalLabel.font      = font;self.highlightedLabel.font = font;self.disabledLabel.font    = font;
}- (void)setVerticalOffset:(CGFloat)verticalOffset {_verticalOffset = verticalOffset;CGRect frame                = self.normalLabel.frame;frame.origin.x              = verticalOffset;self.normalLabel.frame      = frame;self.highlightedLabel.frame = frame;self.disabledLabel.frame    = frame;
}- (void)setHorizontalOffset:(CGFloat)horizontalOffset {_horizontalOffset = horizontalOffset;CGRect frame                = self.normalLabel.frame;frame.origin.y              = horizontalOffset;self.normalLabel.frame      = frame;self.highlightedLabel.frame = frame;self.disabledLabel.frame    = frame;
}@end

控制器源码

//
//  ViewController.m
//  CustomButton
//
//  Created by YouXianMing on 16/5/21.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "CustomButton.h"
#import "UIView+SetRect.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];{CustomButton *button            = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 200, 40.f)];button.title                    = @"Heiti TC";button.center                   = self.view.center;button.y                       -= 100;button.font                     = [UIFont fontWithName:@"Heiti TC" size:16.f];button.layer.borderWidth        = 0.5f;button.layer.borderColor        = [UIColor blackColor].CGColor;button.layer.cornerRadius       = 4.f;button.layer.masksToBounds      = YES;button.buttonEvent              = @selector(buttonsEvent:);button.target                   = self;button.normalBackgroundColor    = [UIColor blackColor];button.highlightBackgroundColor = [UIColor whiteColor];button.disabledBackgroundColor  = [UIColor grayColor];[button setTitleColor:[UIColor whiteColor] state:BaseControlStateNormal];[button setTitleColor:[UIColor blackColor] state:BaseControlStateHighlighted];[button setTitleColor:[UIColor whiteColor] state:BaseControlStateDisabled];[self.view addSubview:button];[button changeToState:BaseControlStateNormal animated:NO];}{CustomButton *button            = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 200, 40.f)];button.title                    = @"Heiti TC";button.tag                      = 2;button.center                   = self.view.center;button.y                       += 100;button.font                     = [UIFont fontWithName:@"Heiti TC" size:16.f];button.layer.borderWidth        = 0.5f;button.layer.borderColor        = [UIColor orangeColor].CGColor;button.layer.cornerRadius       = 4.f;button.layer.masksToBounds      = YES;button.buttonEvent              = @selector(buttonsEvent:);button.target                   = self;button.normalBackgroundColor    = [[UIColor orangeColor] colorWithAlphaComponent:0.95f];button.highlightBackgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.65f];button.disabledBackgroundColor  = [[UIColor orangeColor] colorWithAlphaComponent:0.45f];[button setTitleColor:[UIColor whiteColor] state:BaseControlStateNormal];[button setTitleColor:[UIColor whiteColor] state:BaseControlStateHighlighted];[button setTitleColor:[[UIColor whiteColor] colorWithAlphaComponent:0.75f] state:BaseControlStateDisabled];[self.view addSubview:button];[button changeToState:BaseControlStateNormal animated:NO];}
}- (void)buttonsEvent:(CustomButton *)button {NSLog(@"%@", button);if (button.tag == 2) {static int i = 0;if (i++ >= 3) {[button changeToState:BaseControlStateDisabled animated:YES];[self performSelector:@selector(changeTitle:) withObject:button afterDelay:0.15f];}}
}- (void)changeTitle:(CustomButton *)button {button.title = @"DisabledState";
}@end

核心

转载于:https://www.cnblogs.com/YouXianMing/p/5515909.html

用动画切换按钮的状态相关推荐

  1. android jni示例_Android切换按钮,开关示例

    android jni示例 Today we will learn about Android Toggle Button and Switch in android app. We'll discu ...

  2. android按钮变输入框动画,AnimShopButton 仿饿了么加入购物车旋转控件 - 自带闪转腾挪动画 的按钮。自带海量可定制 UI 属性。在 Re @codeKK Android开源站...

    A shopping cart button with a telescopic displacement rotation animation ... 一个仿饿了么 带伸缩位移旋转动画的购物车按钮 ...

  3. 个人技术总结——Unity中角色动画制作及动画切换逻辑的实现

    这个作业属于哪个课程 软件工程实践2022春-F班 这个作业要求在哪里 软件工程实践总结&个人技术博客 这个作业的目标 课程回顾与总结+个人技术总结 其他参考文献 <Unity2018教 ...

  4. [UE5蓝图基础二]1.[1d混合空间] 走和跑的动画切换(shift切换)

    骨骼网格体与动画类对应 创建混合空间选小金人骨骼 混合空间的概念:混合空间就是可以将角色多个动画混合在一起 ,我们创建动画类就是创建个"Animation"文件夹 在文件夹内创建混 ...

  5. 小程序swiper带切换按钮

    小程序自带swiper组件, display-multiple-items 同时显示的滑块数量 current: 当前所在滑块的 index duration:切换需要花的时间 一般为1秒 bindc ...

  6. 仿饿了么加入购物车旋转控件 - 自带闪转腾挪动画 的按钮

    概述 在上文,酷炫Path动画已经预告了,今天给大家带来的是利用 纯自定义View,实现的仿饿了么加入购物车控件,自带闪转腾挪动画的按钮. 效果图如下: 图1 项目中使用的效果,考虑到了View的回收 ...

  7. android饿了么购物车,Android仿饿了么加入购物车旋转控件自带闪转腾挪动画的按钮效果(实例详解)...

    概述 在上文,酷炫Path动画已经预告了,今天给大家带来的是利用 纯自定义View,实现的仿饿了么加入购物车控件,自带闪转腾挪动画的按钮. 效果图如下: 图1 项目中使用的效果,考虑到了View的回收 ...

  8. 仿饿了么加入购物车旋转控件 - 自带闪转腾挪动画 的按钮。

    AnimShopButton 项目地址:mcxtzhang/AnimShopButton 简介:仿饿了么加入购物车旋转控件 - 自带闪转腾挪动画 的按钮.自带海量可定制 UI 属性.在 Recycle ...

  9. Xamarin.android Activity动画切换效果实现

    http://blog.csdn.net/esunshine1985/article/details/44302903 1.在Resources--values下新建styles.xml,添加内容如下 ...

最新文章

  1. php5.3安装memcache,Windows下的Memcache安装 附php5.3的扩展
  2. 实验mongodb使用gridfs存放一个大文件
  3. Visual Studio Code,这是要上天?
  4. SAP Spartacus 服务器端渲染处理内存泄漏的准则
  5. hdu 1408(高精度)坑人嫩
  6. mysql与citespace_CiteSpace与MySQL数据库的连接-科学网—博客.PDF
  7. iconfont矢量 在已经有的情况下新增图标
  8. 一把误操作卖出500万股,TCL科技李东生致歉:收益归公司
  9. 技术,要拿得起,更要放得下
  10. fseek函数与ftell函数使用例程
  11. android电容触摸屏的驱动及其上层工作原理,电容触摸屏驱动原理
  12. 金笛邮件服务器解析中小企业对自建邮件系统的误区
  13. 自然基金项目爬虫测试(已失效)
  14. VDI IDV VOI
  15. 抖音带货赚钱吗?有哪些技巧?
  16. PrimitiveCollection
  17. 小米是否真的可以干翻华为?
  18. nas linux手机照片备份,本身着手,组建简洁好用的NAS!(存储同步篇)
  19. 做了6年的Java,mysql去重查询方法
  20. AIX小型机安装JAVA JDK的方法

热门文章

  1. Ulink2 No Ulink Device found 解决办法
  2. linux命令之seq
  3. 连载《一个程序猿的生命周期》-2.城市校园生活
  4. Ajax.ActionLink 辅助方法实现局部刷新
  5. sed以及awk的替换命令
  6. C语言的格式控制符问题
  7. AUTOSAR从入门到精通100讲(四十四)-AUTOSAR 通信服务-PDU Router
  8. excel实战应用案例100讲(十二)-用Excel做一个自动抽奖器
  9. 作为产品,看我是如何把技术逼疯的...
  10. 浙大计算机硕士比本科985,二本出身的985研究生与985本科生,哪个更厉害?网友:差的太多!...