BaseControl按钮合集

效果

源码

https://github.com/YouXianMing/Animations

//
// POPBaseControl.h
// Animations
//
// Created by YouXianMing on 16/5/26.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
@class POPBaseControl; @protocol POPBaseControlDelegate <NSObject> /**
* 缩放百分比事件
*
* @param controll PressControll对象
* @param percent 百分比
*/
- (void)POPBaseControl:(POPBaseControl *)controll currentPercent:(CGFloat)percent; /**
* 事件触发
*
* @param controll PressControll对象
*/
- (void)POPBaseControlEvent:(POPBaseControl *)controll; @end @interface POPBaseControl : UIView /**
* 代理
*/
@property (nonatomic, weak) id <POPBaseControlDelegate> delegate; /**
* 动画时间,默认值为0.4
*/
@property (nonatomic) CFTimeInterval animationDuration; /**
* 目标对象
*/
@property (nonatomic, weak) id target; /**
* 事件
*/
@property (nonatomic) SEL selector; /**
* 是否有效
*/
@property (nonatomic) BOOL enabled; /**
* 是否选中
*/
@property (nonatomic) BOOL selected; #pragma mark - Properties used by SubClass & Methods Overwrite by subClass. /**
* 容器view,用于子类添加控件
*/
@property (nonatomic, strong, readonly) UIView *contentView; /**
* 当前动画比例(子类继承的时候重载)
*
* @param percent 比例
*/
- (void)currentPercent:(CGFloat)percent; /**
* 事件激活了
*/
- (void)controllEventActived; @end
//
// POPBaseControl.m
// Animations
//
// Created by YouXianMing on 16/5/26.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "POPBaseControl.h"
#import "POP.h" @interface POPBaseControl () @property (nonatomic, strong) UIView *absView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIView *contentView; @property (nonatomic) CGFloat percent; @end @implementation POPBaseControl - (void)layoutSubviews { [super layoutSubviews];
_button.frame = CGRectMake(, , self.frame.size.width, self.frame.size.height);
_contentView.bounds = CGRectMake(, , self.frame.size.width, self.frame.size.height);
} - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 动画时间
_animationDuration = 0.4f; // 隐身的view
_absView = [[UIView alloc] init];
_absView.userInteractionEnabled = NO;
_absView.backgroundColor = [UIColor clearColor];
[self addSubview:_absView]; // 容器View
_contentView = [[UIView alloc] initWithFrame:self.bounds];
_contentView.userInteractionEnabled = NO;
[self addSubview:_contentView]; // 按钮
_button = [[UIButton alloc] initWithFrame:self.bounds];
[self addSubview:_button]; // 按钮事件
[_button addTarget:self action:@selector(touchBeginOrTouchDragEnter) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
[_button addTarget:self action:@selector(touchUpInside) forControlEvents:UIControlEventTouchUpInside];
[_button addTarget:self action:@selector(touchDragExitOrTouchCancel) forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel];
} return self;
} #pragma mark - Animations. - (void)touchUpInside { [self touchDragExitOrTouchCancel]; [self controllEventActived]; if (self.target && self.selector) { #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self.target performSelector:self.selector withObject:self];
#pragma clang diagnostic pop } if (self.delegate && [self.delegate respondsToSelector:@selector(POPBaseControlEvent:)]) { [self.delegate POPBaseControlEvent:self];
}
} - (void)touchDragExitOrTouchCancel { [_absView.layer pop_removeAllAnimations]; POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
scaleAnimation.toValue = @();
scaleAnimation.delegate = self;
scaleAnimation.duration = _animationDuration;
[_absView.layer pop_addAnimation:scaleAnimation forKey:nil];
} - (void)touchBeginOrTouchDragEnter { [_absView.layer pop_removeAllAnimations]; POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerOpacity];
scaleAnimation.toValue = @();
scaleAnimation.delegate = self;
scaleAnimation.duration = _animationDuration;
[_absView.layer pop_addAnimation:scaleAnimation forKey:nil];
} #pragma mark - POPAnimation's delegate. - (void)pop_animationDidApply:(POPAnimation *)anim { NSNumber *toValue = (NSNumber *)[anim valueForKeyPath:@"currentValue"];
_percent = (toValue.floatValue - [POPBaseControl calculateConstantWithX1: y1: x2: y2:]) / [POPBaseControl calculateSlopeWithX1: y1: x2: y2:]; [self currentPercent:_percent];
} #pragma mark - Overwrite by subClass. - (void)currentPercent:(CGFloat)percent { } - (void)controllEventActived { } #pragma mark - Math. + (CGFloat)calculateSlopeWithX1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 { return (y2 - y1) / (x2 - x1);
} + (CGFloat)calculateConstantWithX1:(CGFloat)x1 y1:(CGFloat)y1 x2:(CGFloat)x2 y2:(CGFloat)y2 { return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
} #pragma mark - setter & getter. @synthesize enabled = _enabled; - (void)setEnabled:(BOOL)enabled { _button.enabled = enabled;
} - (BOOL)enabled { return _button.enabled;
} @synthesize selected = _selected; - (void)setSelected:(BOOL)selected { _button.selected = selected;
} - (BOOL)selected { return _button.selected;
} @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. java 添加自己的工具包

    一. 在添加工具包前环境变量要定位到当前目录, export CLASSPATH=.:/home/share/ 添加工具类 我的目录\\192.168.1.101\share\share\net\fe ...

  2. InnoDB Lock浅谈

    数据库使用锁是为了支持更好的并发,提供数据的完整性和一致性.InnoDB是一个支持行锁的存储引擎,锁的类型有:共享锁(S).排他锁(X).意向共享(IS).意向排他(IX).为了提供更好的并发,Inn ...

  3. MySQL的架构模型

    看到大牛用户DB架构部的Keithlan<数据库性能优化之查询优化>,在学习过程发现很多不错的东西,就把它保存下来,分享给大家,因为作者说了一句很经典的话:“if you want to ...

  4. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

  5. Cname与A记录(Address)区别

    Cname与A记录(Address)区别A记录是解析域名到IP,Cname是解析域名到另外一个域名. 一台服务器可以布置多个网站,也可以有多个域名,如如域名A----->A记录-----> ...

  6. CSS------当内容超出div宽度后自动换行和限制文字不超出div宽度和高度

    如图: 1.自动换行 </div> 2.限制宽高度 </div> (注意:如果div放在li中还需要加上display:inline-block属性)

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

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

  8. 002.RAID创建

    一 创建相应的分区 [root@kauai ~]# fdisk /dev/sdb 最终创建多个用于做RAID的分区,如下: Device Boot Start End Blocks Id System ...

  9. 多线程学习笔记八之线程池ThreadPoolExecutor实现分析

    目录 简介 继承结构 实现分析 ThreadPoolExecutor类属性 线程池状态 构造方法 execute(Runnable command) addWorker(Runnable firstT ...

  10. react运行阶段

    运行中可以使用的函数componentWillReceiveProps:父组件修改属性触发,可以修改新属性,修改状态.字面意思,组件将要接收属性,这个函数触发的时机就是组件的属性将要发生改变的时候,但 ...