Skip to content

Instantly share code, notes, and snippets.

@xremix
Last active March 21, 2017 12:09
Show Gist options
  • Save xremix/4b83938dc6a0eb0c930a5fe38b0678b9 to your computer and use it in GitHub Desktop.
Save xremix/4b83938dc6a0eb0c930a5fe38b0678b9 to your computer and use it in GitHub Desktop.
Swift Round Button with animation
//
// RoundButton.swift
// Toni Hoffmann
//
// Created by Toni Hoffmann on 09.12.16.
// Copyright © 2016 Toni Hoffmann. All rights reserved.
//
// This was created in regards of an Exif Remover App, build with Swift for iPhone
//
import UIKit
extension UIView{
func animateTo(scaleSize: CGFloat){
UIView.animate(withDuration: 0.1, delay: 0, options: [UIViewAnimationOptions.allowUserInteraction, UIViewAnimationOptions.allowAnimatedContent], animations: {
self.transform = CGAffineTransform(scaleX: scaleSize, y: scaleSize)
}, completion: nil)
}
func bobbleTo(scaleSize: CGFloat, strength: CGFloat){
let larger = transform.a <= scaleSize
var bobbleScaleSize = scaleSize * strength
if(!larger){
bobbleScaleSize = scaleSize * (1 / strength)
}
UIView.animate(withDuration: 0.1, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.transform = CGAffineTransform(scaleX: bobbleScaleSize, y: bobbleScaleSize)
}, completion: {
(value: Bool) in
UIView.animate(withDuration: 0.1, animations: {
self.transform = CGAffineTransform(scaleX: scaleSize, y: scaleSize)
})
})
}
func bobbleTo(scaleSize: CGFloat){
bobbleTo(scaleSize: scaleSize, strength: 1.1)
}
}
class BobbleButton: UIButton{
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
// Smooth small animation
animateTo(scaleSize: 0.8)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
// Bubbly bigger aniomation
bobbleTo(scaleSize: 1.0)
}
}
class RoundButton: BobbleButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Disable hightlite status
if buttonType == UIButtonType.custom{
if backgroundImage(for: .normal) == nil{
setBackgroundImage(UIImage(), for: .normal)
}
adjustsImageWhenHighlighted = false
}
}
override func didMoveToSuperview() {
// Always have the correct corner radius
self.layer.cornerRadius = self.bounds.size.width / 2
}
}
class RoundAddButton: RoundButton{
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if(titleLabel == nil){
print("No Title Label")
}
// Adding a beautiful + label
titleEdgeInsets = UIEdgeInsets(top: -10, left: 0, bottom: 0, right: 0)
adjustsImageWhenHighlighted = false
setTitle("+", for: .normal)
titleLabel!.font = UIFont (name: "HelveticaNeue-UltraLight", size: 64)
setTitleColor(UIColor.white, for: .normal)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment