@interface FlatButton : UIButton
重写构造方法
+ (instancetype)button
{
return [self buttonWithType:UIButtonTypeCustom];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
添加响应
//添加按下和进入的响应
[self addTarget:self action:@selector(scaleToSmall)
forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
//添加按下的响应
[self addTarget:self action:@selector(scaleAnimation)
forControlEvents:UIControlEventTouchUpInside];
//添加划出button范围的响应
[self addTarget:self action:@selector(scaleToDefault)
forControlEvents:UIControlEventTouchDragExit];
UIControlEventTouchDragExit 手指离开边界回调
An event where a finger is dragged from within a control to outside its bounds.
但是实际却是离开button边界70左右才回调
接下来使用到pop动画
- (void)scaleToSmall
{
NSLog(@"scaletoSmall");
POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(0.95f, 0.95f)];
[self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSmallAnimation"];
}
- (void)scaleAnimation
{
NSLog(@"scaleAnimation");
POPSpringAnimation *scaleAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.velocity = [NSValue valueWithCGSize:CGSizeMake(6.f, 6.f)];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
scaleAnimation.springBounciness = 22.0f;
[self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleSpringAnimation"];
}
- (void)scaleToDefault
{
NSLog(@"scaletoDefault");
POPBasicAnimation *scaleAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.f, 1.f)];
[self.layer pop_addAnimation:scaleAnimation forKey:@"layerScaleDefaultAnimation"];
}
设置一下标题偏移
- (UIEdgeInsets)titleEdgeInsets
{
//top, left, bottom, right
return UIEdgeInsetsMake(4.f,
28.f,
4.f,
28.f);
}
控件的内置大小 --- 由控件本身的内容所决定的
- (CGSize)intrinsicContentSize
{
CGSize s = [super intrinsicContentSize];
return CGSizeMake(s.width + self.titleEdgeInsets.left + self.titleEdgeInsets.right,
s.height + self.titleEdgeInsets.top + self.titleEdgeInsets.bottom);
}