首先来看看实现后的效果吧
实现思路
其实这个按钮的实现时分简单,我的思路是:
- 用UIView + UITapGestureRecognizer 来模拟实现一个按钮的效果
- 记录每次手指点击的位置,以该位置为圆心用CALayer画圆
- 采用block回调实现点击事件
- 采用CAAnimationGroup来实现组合动画(涟漪效果)
- 最后在CAAnimationDelegate中- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;方法中消除动画。
是不是很简单?
下面我们来看下代码实现
//在ZYCRippleButton.h文件中
#import <UIKit/UIKit.h>
typedef void (^ZYCRippleButtonBlock)(void);
@interface ZYCRippleButton : UIView
//用于模拟button的tittle
@property (nonatomic, strong) UILabel *textLabel;
//“涟漪”颜色
@property (nonatomic, strong) UIColor *rippleColor;
//“涟漪”的粗细
@property (nonatomic, assign) NSUInteger rippleLineWidth;
//点击回调block
@property (nonatomic, copy) ZYCRippleButtonBlock rippleBlock;
- (void) setButtonTittle:(NSString *)tittle;
- (void) setButtonTittleColor:(UIColor *)tColor;
- (void) setButtonBackgroundColor:(UIColor *)bgColor;
- (void) setButtonTittle:(NSString *)tittle withTittleColor:(UIColor *)tColor;
- (void) setButtonTittle:(NSString *)tittle withTittleColor:(UIColor *)tColor backgroundColor:(UIColor *)bgColor;
@end
//在ZYCRippleButton.m文件中
#import "ZYCRippleButton.h"
const CGFloat ZYCRippleInitialRaius = 20;
@interface ZYCRippleButton()<CAAnimationDelegate>
@end
@implementation ZYCRippleButton
- (instancetype)initWithFrame:(CGRect)frame{
if<