Skip to content

Instantly share code, notes, and snippets.

@cdzombak
Created January 21, 2017 16:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdzombak/87e5a9d7533dc2cb5c4d004021f12b95 to your computer and use it in GitHub Desktop.
Save cdzombak/87e5a9d7533dc2cb5c4d004021f12b95 to your computer and use it in GitHub Desktop.
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