用动画切换按钮的状态

用动画切换按钮的状态

 

效果

 

源码

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,一经查实,立即删除!

相关文章

iOS开发之学前了解

学iOS开发能做什么&#xff1f; iOS开发需要学习哪些内容&#xff1f; 先学习什么&#xff1f; 不管你是学习android开发还是iOS开发 都建议先学习UI&#xff0c;原因如下&#xff1a; UI是app的根基&#xff1a;一个app应该是先有UI界面&#xff0c;然后在UI的基础上增加实用功…

力扣gupiao

给定一个数组 prices &#xff0c;它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。 你只能选择 某一天 买入这只股票&#xff0c;并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。 返回你可以从这笔交易中获取的最大利润。…

Java相当好的隐私(PGP)

公钥加密 这篇文章讨论了PGP或“很好的隐私”。 PGP是常规加密和公用密钥加密的混合实现。 在详细介绍PGP之前&#xff0c;让我们先谈谈公钥加密。 与其他任何加密技术一样&#xff0c;公钥加密解决了通过不安全介质传输安全数据的问题。 即互联网。 结果&#xff0c;该方案的…

HDU 5691 Sitting in Line 状压dp

Sitting in Line题目连接&#xff1a; http://acm.hdu.edu.cn/showproblem.php?pid5691 Description 度度熊是他同时代中最伟大的数学家&#xff0c;一切数字都要听命于他。现在&#xff0c;又到了度度熊和他的数字仆人们玩排排坐游戏的时候了。游戏的规则十分简单&#xff0c…

hello oc

printf("Hello C\n"); //OC可以采用C语言的输出方式 printf("The number is %d\n",100);//%d 输出数字 printf("Hello %s\n","XiaoMing");//%s 输出字符 NSLog("Hello Objective-C"); //采用oc的输出&#xff0c;前面带了一…

Spring3 RESTful Web服务

Spring 3提供了对RESTful Web服务的支持。 在本教程中&#xff0c;我们将向您展示如何在Spring中实现RESTful Web服务 &#xff0c;或者如何将现有的Spring服务公开为RESTful Web服务 。 为了使事情变得更有趣&#xff0c;我们将从上一篇关于Spring GWT Hibernate JPA Infinisp…

zoj 3765 块状链表 OR splay

各种操作o(╯□╰)o...不过都挺简单&#xff0c;不需要lazy标记。 方法1&#xff1a;块状链表 块状链表太强大了&#xff0c;区间操作实现起来简单暴力&#xff0c;效率比splay稍微慢一点&#xff0c;内存开销小很多。 1 #include <iostream>2 #include <cstring>3…

【C#公共帮助类】 Image帮助类

大家知道&#xff0c;开发项目除了数据访问层很重要外&#xff0c;就是Common了&#xff0c;这里就提供了强大且实用的工具。 【C#公共帮助类】 Convert帮助类 Image类&#xff1a; using System; using System.Collections.Generic; using System.Text; using System.IO; usin…

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…

Hive查询Join

Select a.val,b.val From a [Left|Right|Full Outer] Join b On (a.keyb.key); 现有两张表&#xff1a;sales 列出了人名及其所购商品的 ID&#xff1b;things 列出商品的 ID 和名称&#xff1a; hive> select * from sales; OK Joe 2 Hank 4 Ali 0 Eve 3 Ha…

jquery 获取easyui combobox选中的值

$(#comboboxlist).combobox(getValue);转载于:https://www.cnblogs.com/ftm-datablogs/p/5526857.html

调度Java应用程序中的主体

许多项目需要计划功能&#xff0c;例如我们计划的工作&#xff0c;重复的工作&#xff0c;异步执行等。 我们的首选方法是使用企业工作调度程序&#xff0c;例如OpenSymphony的Quartz。 使用计划任务进行编码时&#xff0c;最棘手的部分之一是执行部分。 这里的主要经验法则是…

继承映射关系 joinedsubclass的查询

会出现下面这样的错一般是配置文件中的mapping和映射文件中的package路径或者class中的name路径不一致 org.hibernate.MappingException: Unknown entity: com.zh.hibernate.joinedsubclass.Student at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(Sessi…

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…

网页前台小知识

1.左右布局div块自适应&#xff0c;首先外边套一个div,把宽度固定一个px&#xff0c;然后margin设为&#xff10; atuo&#xff1b;这样他会根据窗口大小自动变换左右距离&#xff0e;就这么简单&#xff1c;/p> 2.多个标签共用一个样式&#xff0c;用&#xff0c;分隔开 p…

统计字符串每个字符出现的次数

//str是个只包含小写字母的字符串&#xff0c;以下是统计每个字符出现的频数 int[] cnt new int[26];//toCharArray() for (char ch : str.toCharArray()) {cnt[ch - a]; }//charAt() for(int i 0;i<str.length;i){char ch str.charAt(i);cnt[ch - a]; }

在Java 7中处理文件

以下是The Well-Grounded Java Developer的草稿的修改后的片段。 它使您快速了解与以前版本相比&#xff0c;在Java 7中操作文件要容易得多。 通过使用新的Files类及其许多实用程序方法&#xff0c;您可以仅用一行代码就可以对文件执行以下操作&#xff1a; 创建 删除 复制 …

3.1存储管理操作系统

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