用動畫切換按鈕的狀態

用動畫切換按鈕的狀態

?

效果

?

源碼

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.pswp.cn/news/374638.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/374638.shtml
英文地址,請注明出處:http://en.pswp.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;、物理地…