用动画切换按钮的状态

用动画切换按钮的状态

 

效果

 

源码

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/374638.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java泛型快速教程

泛型是Java SE 5.0引入的一种Java功能&#xff0c;在其发布几年后&#xff0c;我发誓那里的每个Java程序员不仅听说过它&#xff0c;而且已经使用过它。 关于Java泛型&#xff0c;有很多免费和商业资源&#xff0c;而我使用的最佳资源是&#xff1a; Java教程 Java泛型和集合…

876. 链表的中间结点

给定一个头结点为 head 的非空单链表&#xff0c;返回链表的中间结点。 如果有两个中间结点&#xff0c;则返回第二个中间结点 代码一&#xff1a; 自己想的一个方法 class Solution {public ListNode middleNode(ListNode head) {ListNode p1 head;ListNode p2 head;//i,j…

Spark系列—02 Spark程序牛刀小试

一、执行第一个Spark程序 1、执行程序 我们执行一下Spark自带的一个例子&#xff0c;利用蒙特卡罗算法求PI&#xff1a; 启动Spark集群后&#xff0c;可以在集群的任何一台机器上执行一下命令&#xff1a; /home/spark/spark-1.6.1-bin-hadoop2.6/bin/spark-submit \ --class o…

JVM选项:-client vs -server

您是否曾经在运行Java应用程序时想知道-client或-server开关是什么&#xff1f; 例如&#xff1a; javaw.exe -client com.blogspot.sdoulger.LoopTest也显示在java.exe的“帮助”中&#xff0c;例如&#xff0c;其中的选项包括&#xff1a; -client选择“客户端” VM -serv…

3.1存储管理操作系统

存储器管理的对象是主存&#xff08;内存&#xff09;。其主要功能包含分配和回收主存空间、提高主存的利用率、扩充主存、对主存信息实现有效保护。存储器的结构为&#xff1a;寄存去、缓存、主存、外存。逻辑地址&#xff08;对用户角度。程序存放的位置&#xff09;、物理地…

了解和扩展Java ClassLoader

Java ClassLoader是项目开发中Java的关键但很少使用的组件之一。 就我个人而言&#xff0c;我从未在任何项目中扩展ClassLoader&#xff0c;但是拥有自己的可以自定义Java类加载的ClassLoader的想法让我感到很兴奋。 本文将概述Java类加载&#xff0c;然后继续创建自定义ClassL…

CAD教程-AL对其命令

AL可以实现不规则的对其功能 1.第一步按下AL&#xff0c;按下Enter 2.选择第一个源点 3.选择第一个目标点 4.选择第二个源点 5.选择第二个目标点 6.按下Enter&#xff0c;完成移位 转载于:https://www.cnblogs.com/weloveshare/p/4739873.html

(一二四)tableView的多组数据展示和手动排序

最近在写一个轻量级的网络游戏&#xff0c;遇到了技能优先顺序手动排序的需求&#xff0c;我就想到了iOS自带的tableView编辑功能&#xff0c;对其进行了初步探索&#xff0c;最后做出的效果如下图所示&#xff1a; 点击左边可以删除&#xff0c;拖住右边可以手动排序&#xff…

知道这 20 个正则表达式,能让你少写 1,000 行代码

CocoaChina05-13正则表达式&#xff0c;一个十分古老而又强大的文本处理工具&#xff0c;仅仅用一段非常简短的表达式语句&#xff0c;便能够快速实现一个非常复杂的业务逻辑。熟练地掌握正则表达式的话&#xff0c;能够使你的开发效率得到极大的提升。下面是技匠整理的&#x…

元素分类--块级元素(特点:独占一行, 宽高边距可改)

什么是块级元素&#xff1f;在html中<div>、 <p>、<h1>、<form>、<ul> 和 <li>就是块级元素。设置display:block就是将元素显示为块级元素。如下代码就是将内联元素a转换为块状元素&#xff0c;从而使a元素具有块状元素特点。 a{display:b…

站立会议05(第二次冲刺)

一、站立会议信息&#xff08;配站立会议照片&#xff09; 第五天我们继续开发&#xff0c;把注册验证信息完善一下&#xff0c;将开始网站公共主页的开发。 二、任务进度 第五天我们注册验证完成。 三、任务看板&#xff08;图&#xff09; 四、燃尽图&#xff08;图&#xff…

[SoapUI] DataSource, DataSourceLoop, DataSink

Script assertion in login: 转载于:https://www.cnblogs.com/MasterMonkInTemple/p/4748189.html

将CAPTCHA添加到您的GWT应用程序

什么是验证码&#xff1f; 在一个充满恶意机器人的世界中&#xff0c;您该怎么做才能保护您宝贵的Web应用程序&#xff1f; 您真正应该做的基本事情之一就是向其中添加CAPTCHA功能。 如果您不熟悉&#xff08;听起来有些奇怪&#xff09;&#xff0c;则CAPTCHA是确保用户实际上…

SQL基础语句

数据库面试常见题 一、SQL语言包括数据定义语言、数据操作语言、数据控制语言和事务控制语言1&#xff1a;DDL(Data Definition Language)&#xff0c;是用于描述数据库中要存储的现实世界实体的语言。 CREATE TABLE - 创建新表 ALTER TABLE - 变更&#xff08;改变&#xff0…

iOS学习——ScrollView图片轮播和同类控件优先级问题

iOS学习——ScrollView的使用和同类控件优先级问题 1. 布置界面 ScrollView的使用非常简单&#xff0c;只有三步 1.1 添加一个scrollview 1.2 向scrollview添加内容 1.3 告诉scrollview中内容的实际大小 首先做第一步&#xff0c;布置界面。 拖拽一个scrollview就可以了 就…

Git 分支管理和冲突解决

创建分支 git branch 没有参数&#xff0c;显示本地版本库中所有的本地分支名称。 当前检出分支的前面会有星号。 git branch newname 在当前检出分支上新建分支&#xff0c;名叫newname。 git checkout newname 检出分支&#xff0c;即切换到名叫newname的分支。 git checkout…

git克隆/更新/提交代码步骤及示意图

1. git clone ssh://flycm.intel.com/scm/at/atSrc 或者git clone ssh://flycm.intel.com/scm/at/atJar 或者git clone ssh://flycm.intel.com/scm/at/atFramework 2. git checkout cpeg/scm/stable 切换分支&#xff0c;然后更新代码 3. git pull 先把远程分支上最新的代码拉到…

互联网金融P2P主业务场景自动化测试

互联网金融P2P行业&#xff0c;近三年来发展迅速&#xff0c;如火如荼。据不完全统计&#xff0c;全国有3000的企业。“互联网”企业&#xff0c;几乎每天都会碰到一些奇奇怪怪的bug&#xff0c;作为在互联网企业工作的测试人员&#xff0c;风险和压力都巨大。那么我们如何降低…

OSGi将Maven与Equinox结合使用

很长时间以来&#xff0c;我一直在努力理解OSGi的真正含义。 它已经存在很长时间了&#xff0c;但是没有多少人意识到这一点。 人们已经大肆宣传它是一种非常复杂的技术。 这是我为所有Java开发人员简化的尝试。 简而言之&#xff0c; OSGi是一组规范&#xff0c;这些规范允许对…

Hadoop:简单介绍

什么是Hadoop&#xff1a; Hadoop是一种用Java编写的框架&#xff0c;用于在大型商品硬件集群上运行应用程序&#xff0c;并具有类似于Google File System和MapReduce的功能 。 HDFS是高度容错的分布式文件系统&#xff0c;与Hadoop一样&#xff0c;旨在部署在低成本硬件上。 它…