Skip to content

Instantly share code, notes, and snippets.

@bih
Created August 15, 2015 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bih/bbed3d7127f38ca42f89 to your computer and use it in GitHub Desktop.
Save bih/bbed3d7127f38ca42f89 to your computer and use it in GitHub Desktop.

BHSwipeButton

BHSwipeButton is a subclass of UIButton that allows you within a couple of lines to add a UIButton with a shimmer and the ability for a user to swipe it right in order to perform an action. I wrote it whilst building an iOS application and thought it might be useful in your project.

How to use

  • Import Facebook's Shimmer Objective-C project (supported on Cocoapods.org)
  • Import BHSwipeButton.swift in this gist to your XCode project (must be a version supporting Swift 1.2 or above)
  • Once imported, on your storyboard or XIB files set the class for the UIButton you want to customize with BHSwipeButton
  • Within your UIViewController class, you should link your button to delegate = self and use setText("Swipe to do something") to set text
  • You should also have a BHSwipeButtonDelegate attached
  • Once you have that, you'll need to implement a function called func swipeButton(button: BHSwipeButton, userDidSwipeButton animated: Bool)
  • That's it! Whenever the user swipes,
class ExampleController : UIViewController, BHSwipeButtonDelegate {
  @IBAction var finishSignupButton : BHSwipeButton!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    self.finishSignupButton.delegate = self
    self.finishSignupButton.setTitle("Swipe to finish signup")
  }
  
  func swipeButton(button: BHSwipeButton, userDidSwipeButton animated: Bool) {
    // User swiped. Sign them up here!
  }
}
//
// BHSwipeButton.swift
// HelloHub
//
// Created by Bilawal Hameed on 09/08/2015.
// Copyright (c) 2015 HelloHub. All rights reserved.
//
import Foundation
import UIKit
import Shimmer
protocol BHSwipeButtonDelegate : NSObjectProtocol {
func swipeButton(button: BHSwipeButton, userDidSwipeButton animated: Bool)
}
class BHSwipeButton : UIButton {
weak var delegate : BHSwipeButtonDelegate!
private var defaultPositionX : CGFloat!
private var userWantsToPerformAction : Bool = false
private var shimmeringView : FBShimmeringView?
var shimmeringTitleLabel : UILabel!
var buttonText : String = "" {
didSet { shimmeringTitleLabel.text = buttonText }
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// add gesture
let panGesture = UIPanGestureRecognizer(target: self, action: "respondToPan:")
addGestureRecognizer(panGesture)
// set bounds
setBounds(width: bounds.width, height: bounds.height)
}
func setBounds(#width: CGFloat, height: CGFloat) -> Void {
// clear if exists
if shimmeringView != nil {
subviews[0].removeFromSuperview()
}
// set bound
bounds = CGRect(
x: 0,
y: 0,
width: width,
height: height
)
// set default position
shimmeringView = FBShimmeringView(frame: bounds)
defaultPositionX = shimmeringView!.layer.position.x
// set label
shimmeringTitleLabel = UILabel(frame: shimmeringView!.bounds)
shimmeringTitleLabel.text = buttonText
shimmeringTitleLabel.textColor = UIColor.whiteColor()
shimmeringTitleLabel.font = UIFont.systemFontOfSize(14.0)
shimmeringTitleLabel.textAlignment = NSTextAlignment.Center
// set content view
shimmeringView!.contentView = shimmeringTitleLabel
shimmeringView!.shimmering = true
defaultPositionX = shimmeringView!.layer.position.x
addSubview(shimmeringView!)
}
func setTitle(title: String?) {
setTitle(title, forState: .Normal)
}
override func setTitle(title: String?, forState state: UIControlState) {
buttonText = title!
}
func respondToPan(sender : UIPanGestureRecognizer) {
let translation = sender.translationInView(self)
var moveTo = defaultPositionX + translation.x
if moveTo < defaultPositionX { moveTo = defaultPositionX }
(subviews[0] as! FBShimmeringView).layer.position.x = moveTo
if translation.x > 150 {
userWantsToPerformAction = true
}
if sender.state == UIGestureRecognizerState.Ended {
(subviews[0] as! FBShimmeringView).layer.position.x = defaultPositionX
if userWantsToPerformAction == true {
userWantsToPerformAction = false
if delegate != nil {
delegate!.swipeButton(self, userDidSwipeButton: true)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment