Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A UIButton with a nice scaling effect when the user touches down on it.
//
// CDZScalingButton.h
// ScalingButton
//
// Created by Chris Dzombak on 10/17/15.
// Copyright © 2015 Chris Dzombak. All rights reserved.
//
@import UIKit;
@interface CDZScalingButton : UIButton
@end
//
// CDZScalingButton.h
// ScalingButton
//
// Created by Chris Dzombak on 10/17/15.
// Copyright © 2015 Chris Dzombak. All rights reserved.
//
@import QuartzCore;
#import "CDZScalingButton.h"
@implementation CDZScalingButton
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self commonInit];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self commonInit];
}
return self;
}
- (void)commonInit {
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.imageView.contentMode = UIViewContentModeCenter;
}
- (void)setHighlighted:(BOOL)highlighted {
if (highlighted != self.highlighted) {
CGFloat targetScale = highlighted ? 0.85f : 1.0f;
NSTimeInterval duration = highlighted ? 0.3f : 0.2f;
[UIView animateWithDuration:duration
delay:0.05
options:UIViewAnimationOptionCurveEaseOut
animations:^{
if (self.titleLabel.text.length) {
self.titleLabel.layer.transform = CATransform3DMakeScale(targetScale, targetScale, 1.0f);
}
else if (self.imageView.image) {
self.imageView.layer.transform = CATransform3DMakeScale(targetScale, targetScale, 1.0f);
}
}
completion:nil];
}
[super setHighlighted:highlighted];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment