BaseControl按钮合集

效果

源码

https://github.com/YouXianMing/Animations

  1. //
  2. // POPBaseControl.h
  3. // Animations
  4. //
  5. // Created by YouXianMing on 16/5/26.
  6. // Copyright © 2016年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10. @class POPBaseControl;
  11.  
  12. @protocol POPBaseControlDelegate <NSObject>
  13.  
  14. /**
  15. * 缩放百分比事件
  16. *
  17. * @param controll PressControll对象
  18. * @param percent 百分比
  19. */
  20. - (void)POPBaseControl:(POPBaseControl *)controll currentPercent:(CGFloat)percent;
  21.  
  22. /**
  23. * 事件触发
  24. *
  25. * @param controll PressControll对象
  26. */
  27. - (void)POPBaseControlEvent:(POPBaseControl *)controll;
  28.  
  29. @end
  30.  
  31. @interface POPBaseControl : UIView
  32.  
  33. /**
  34. * 代理
  35. */
  36. @property (nonatomic, weak) id <POPBaseControlDelegate> delegate;
  37.  
  38. /**
  39. * 动画时间,默认值为0.4
  40. */
  41. @property (nonatomic) CFTimeInterval animationDuration;
  42.  
  43. /**
  44. * 目标对象
  45. */
  46. @property (nonatomic, weak) id target;
  47.  
  48. /**
  49. * 事件
  50. */
  51. @property (nonatomic) SEL selector;
  52.  
  53. /**
  54. * 是否有效
  55. */
  56. @property (nonatomic) BOOL enabled;
  57.  
  58. /**
  59. * 是否选中
  60. */
  61. @property (nonatomic) BOOL selected;
  62.  
  63. #pragma mark - Properties used by SubClass & Methods Overwrite by subClass.
  64.  
  65. /**
  66. * 容器view,用于子类添加控件
  67. */
  68. @property (nonatomic, strong, readonly) UIView *contentView;
  69.  
  70. /**
  71. * 当前动画比例(子类继承的时候重载)
  72. *
  73. * @param percent 比例
  74. */
  75. - (void)currentPercent:(CGFloat)percent;
  76.  
  77. /**
  78. * 事件激活了
  79. */
  80. - (void)controllEventActived;
  81.  
  82. @end
  1. //
  2. // POPBaseControl.m
  3. // Animations
  4. //
  5. // Created by YouXianMing on 16/5/26.
  6. // Copyright © 2016年 YouXianMing. All rights reserved.
  7. //
  8.  
  9. #import "POPBaseControl.h"
  10. #import "POP.h"
  11.  
  12. @interface POPBaseControl ()
  13.  
  14. @property (nonatomic, strong) UIView *absView;
  15. @property (nonatomic, strong) UIButton *button;
  16. @property (nonatomic, strong) UIView *contentView;
  17.  
  18. @property (nonatomic) CGFloat percent;
  19.  
  20. @end
  21.  
  22. @implementation POPBaseControl
  23.  
  24. - (void)layoutSubviews {
  25.  
  26. [super layoutSubviews];
  27. _button.frame = CGRectMake(, , self.frame.size.width, self.frame.size.height);
  28. _contentView.bounds = CGRectMake(, , self.frame.size.width, self.frame.size.height);
  29. }
  30.  
  31. - (instancetype)initWithFrame:(CGRect)frame {
  32.  
  33. if (self = [super initWithFrame:frame]) {
  34.  
  35. // 动画时间
  36. _animationDuration = 0.4f;
  37.  
  38. // 隐身的view
  39. _absView = [[UIView alloc] init];
  40. _absView.userInteractionEnabled = NO;
  41. _absView.backgroundColor = [UIColor clearColor];
  42. [self addSubview:_absView];
  43.  
  44. // 容器View
  45. _contentView = [[UIView alloc] initWithFrame:self.bounds];
  46. _contentView.userInteractionEnabled = NO;
  47. [self addSubview:_contentView];
  48.  
  49. // 按钮
  50. _button = [[UIButton alloc] initWithFrame:self.bounds];
  51. [self addSubview:_button];
  52.  
  53. // 按钮事件
  54. [_button addTarget:self action:@selector(touchBeginOrTouchDragEnter) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
  55. [_button addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
  56. [_button addTarget:self action:@selector(touchDragExitOrTouchCancel) forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel];
  57. }
  58.  
  59. return self;
  60. }
  61.  
  62. #pragma mark - Animations.
  63.  
  64. - (void)touchUpInside {
  65.  
  66. [self touchDragExitOrTouchCancel];
  67.  
  68. [self controllEventActived];
  69.  
  70. if (self.target && self.selector) {
  71.  
  72. #pragma clang diagnostic push
  73. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  74. [self.target performSelector:self.selector withObject:self];
  75. #pragma clang diagnostic pop
  76.  
  77. }
  78.  
  79. if (self.delegate && [self.delegate respondsToSelector:@selector(POPBaseControlEvent:)]) {
  80.  
  81. [self.delegate POPBaseControlEvent:self];
  82. }
  83. }
  84.  
  85. - (void)touchDragExitOrTouchCancel {
  86.  
  87. [_absView.layer pop_removeAllAnimations];
  88.  
  89. POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
  90. scaleAnimation.toValue = @();
  91. scaleAnimation.delegate = self;
  92. scaleAnimation.duration = _animationDuration;
  93. [_absView.layer pop_addAnimation:scaleAnimation forKey:nil];
  94. }
  95.  
  96. - (void)touchBeginOrTouchDragEnter {
  97.  
  98. [_absView.layer pop_removeAllAnimations];
  99.  
  100. POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
  101. scaleAnimation.toValue = @();
  102. scaleAnimation.delegate = self;
  103. scaleAnimation.duration = _animationDuration;
  104. [_absView.layer pop_addAnimation:scaleAnimation forKey:nil];
  105. }
  106.  
  107. #pragma mark - POPAnimation's delegate.
  108.  
  109. - (void)pop_animationDidApply:(POPAnimation *)anim {
  110.  
  111. NSNumber *toValue = (NSNumber *)[anim valueForKeyPath:@"currentValue"];
  112. _percent = (toValue.floatValue - [POPBaseControl calculateConstantWithX1: y1: x2: y2:]) / [POPBaseControl calculateSlopeWithX1: y1: x2: y2:];
  113.  
  114. [self currentPercent:_percent];
  115. }
  116.  
  117. #pragma mark - Overwrite by subClass.
  118.  
  119. - (void)currentPercent:(CGFloat)percent {
  120.  
  121. }
  122.  
  123. - (void)controllEventActived {
  124.  
  125. }
  126.  
  127. #pragma mark - Math.
  128.  
  129. + (CGFloat)calculateSlopeWithX1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 {
  130.  
  131. return (y2 - y1) / (x2 - x1);
  132. }
  133.  
  134. + (CGFloat)calculateConstantWithX1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 {
  135.  
  136. return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
  137. }
  138.  
  139. #pragma mark - setter & getter.
  140.  
  141. @synthesize enabled = _enabled;
  142.  
  143. - (void)setEnabled:(BOOL)enabled {
  144.  
  145. _button.enabled = enabled;
  146. }
  147.  
  148. - (BOOL)enabled {
  149.  
  150. return _button.enabled;
  151. }
  152.  
  153. @synthesize selected = _selected;
  154.  
  155. - (void)setSelected:(BOOL)selected {
  156.  
  157. _button.selected = selected;
  158. }
  159.  
  160. - (BOOL)selected {
  161.  
  162. return _button.selected;
  163. }
  164.  
  165. @end

说明

本人一共封装了3种按钮的基类控件,以上是一个示例演示,演示如何通过继承来实现想要的效果.

BaseControl按钮合集的更多相关文章

  1. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  2. 不容错过的UI设计素材大合集

    免费PSD素材 TETHR by InVision 这是出自InVision的8款PSD文件,其中包含了100个模板和超过500个UI控件.来自InVision和UI8的设计师一同协作完成了这套UI ...

  3. Android中的对话框AlertDialog使用技巧合集-转载

    Android中的对话框AlertDialog使用技巧合集     文章来自:http://blog.csdn.net/blue6626/article/details/6641105   今天我用自 ...

  4. Web测试到底是在测什么(资料合集)

    开始今晚的主题之前 先来看一张图, 这是老徐16年10月份,线上Web主题分享时整理的大纲 图片略模糊 看得清就好 Web测试, 进行抽离拆分,基本上就如上一些内容. 不管是测什么系统,什么功能,基本 ...

  5. FFmpeg示例程序合集-批量编译脚本

    此前做了一系列有关FFmpeg的示例程序,组成了<最简单的FFmpeg示例程序合集>,其中包含了如下项目:simplest ffmpeg player:                   ...

  6. 【Oracle教程资源大合集】Oracle数据库免费学习资源汇总

    Oracle的产品非常丰富,各类学习资源也五花八门,本文将介绍Oracle官方的免费教程与风哥整理的Oracle视频教程: 1.Oracle帮助中心 Oracle帮助中心也称为Oracle文档中心,这 ...

  7. 24个 CSS 高级技巧合集

    上期入口:史上最全实用网络爬虫合集! 1.使用CSS复位 CSS复位可以在不同的浏览器上保持一致的样式风格.您可以使用CSS reset 库Normalize等,也可以使用一个更简化的复位方法: ** ...

  8. Power BI 3-4月功能更新培训合集

    ​Power BI 3-4月功能更新培训合集 Power BI每月功能的更新,都有很多大咖精辟解读,我们一直也都是积极中期待,相信所有P友如是或更甚. 视频学习可以结合微软Power BI 3-4月文 ...

  9. 常用的js代码合集

    !function(util){ window.Utils = util(); }( function(){ //document_event_attributes var DEA = "d ...

随机推荐

  1. Centos7下yum安装zabbix-server的部署(一)

    一.环境准备 OS:CentOS 7.2 64bit Zabbix版本:3.0.12 MySQL版本:5.6 hostname ip 主机用途zabbix-server 10.0.0.44 服务端 z ...

  2. DDD领域模型企业级系统(三)

    相关代码: public static void ShowArray() { //数据源 int[] arrayas = new int[] { 1, 2, 3, 4 }; //创建查询 var qu ...

  3. List集合去除重复对象及equals()、hashCode()方法的作用

    原文:https://blog.csdn.net/freelander_j/article/details/52211010 在java中,要将一个集合中重复的对象除去,如果这个集合中的数据类型是基本 ...

  4. 【LOJ】#2270. 「SDOI2017」天才黑客

    题解 显然要记录每个点来的状态,这样会扩充出点度的平方条边,就gg了 删掉所有的点,把每个边拆成两个点,连一条边权为c 这个时候我们考虑对于原先的每个点,将所有与其相连边所需要的节点(不管是进入还是出 ...

  5. 如何快速的打开当前文件夹的dos命令窗口

    一.常规方法: 1.使用 “window + R” 组合键,输入cmd回车.如下图所示: 2.如果你要定位到指定的文件夹,那么需要用cd等命令来处理.如下图所示: 二.快速方法: 按住“shift”键 ...

  6. Sqoop的安装及简单使用

    SQOOP是用于对数据进行导入导出的. (1)把MySQL.Oracle等数据库中的数据导入到HDFS.Hive.HBase中   (2)把HDFS.Hive.HBase中的数据导出到MySQL.Or ...

  7. Chrome浏览器被hao123劫持,浏览器主页会被篡改为 hao123等

    先放一个知乎帖子: https://www.zhihu.com/question/21883209 我就只放几个有效解决办法了,具体的可以看上边那个帖子 方案一:     删掉桌面上的chrome图标 ...

  8. WinSCP命令行操作

    WinSCP命令行操作     WinSCP是一个Windows环境下使用SSH的开源图形化SFTP客户端.同时支持SCP协议.它的主要功能就是在本地与远程计算机间安全的复制文件. 直接在cmd下输入 ...

  9. 洛谷P2261 [CQOI2007] 余数求和 [数论分块]

    题目传送门 余数求和 题目背景 数学题,无背景 题目描述 给出正整数n和k,计算G(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod ...

  10. Java日期时间类

    日期时间类有三种: 一.java.util.Date:一般用于声明日期时间类型的变量. 二.java.sql.Date:一般用于数据库日期时间的映射. 三.java.util.Calendar:一般用 ...