Skip to content

Instantly share code, notes, and snippets.

View victorBaro's full-sized avatar
👾

Victor Baro victorBaro

👾
View GitHub Profile
@victorBaro
victorBaro / CustomDynamicAlertView
Created February 23, 2015 09:52
Small class (Alert View) we made during third week at ironhack bootcamp using UIKit Dynamics. http://victorbaro.com/2015/02/custom-dynamic-alertview/
//
// CustomDynamicAlertView.h
// Week3_inclass
//
// Created by Victor Baro on 18/02/2015.
// Copyright (c) 2015 Produkt. All rights reserved.
//
#import <UIKit/UIKit.h>
@victorBaro
victorBaro / AnimatedLabel
Created March 22, 2015 22:44
Swift test from original Nick Lockwood article: https://gist.github.com/nicklockwood/d374033b27c62662ac8d (animated UILabel on text changes)
import UIKit
class ViewController: UIViewController {
let magicLabel = AnimatedLabel(frame: CGRectMake(100, 100, 300, 20));
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
magicLabel.text = "Magic label"
self.view.addSubview(magicLabel);
@IBAction func viewDragged(sender: UIPanGestureRecognizer) {
let yTranslation = sender.translationInView(view).y
if (hasExceededVerticalLimit(topViewConstraint.constant)){
totalTranslation += yTranslation
topViewConstraint.constant = logConstraintValueForYPoisition(totalTranslation)
if(sender.state == UIGestureRecognizerState.Ended ){
animateViewBackToLimit()
}
} else {
topViewConstraint.constant += yTranslation
@victorBaro
victorBaro / ForceGestureRecognizer.swift
Last active May 5, 2018 21:29
Custom GestureRecognizer for Force Touch
import UIKit.UIGestureRecognizerSubclass
class ForceGestureRecognizer: UIGestureRecognizer {
var forceValue: CGFloat = 0
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
state = .Began
handleForceWithTouches(touches)
@victorBaro
victorBaro / ForceButton.swift
Created October 18, 2015 21:10
Function for controlling button's shadow based on input force
func shadowWithAmount(amount: CGFloat) {
self.layer.shadowColor = shadowColor.CGColor
self.layer.shadowOpacity = shadowOpacity
let widthFactor = maxShadowOffset.width/maxForceValue
let heightFactor = maxShadowOffset.height/maxForceValue
self.layer.shadowOffset = CGSize(width: maxShadowOffset.width - amount * widthFactor, height: maxShadowOffset.height - amount * heightFactor)
self.layer.shadowRadius = maxShadowRadius - amount
}
@victorBaro
victorBaro / imagePressed.swift
Last active May 18, 2016 02:52
Zoom image while being pressed
func imagePressed(sender: ForceGestureRecognizer) {
let point = sender.locationInView(self.view)
let imageCoordPoint = CGPointMake(point.x - initialFrame.origin.x, point.y - initialFrame.origin.y)
var xValue = max(0, imageCoordPoint.x / initialFrame.size.width)
var yValue = max(0, imageCoordPoint.y / initialFrame.size.height)
xValue = min(xValue, 1)
yValue = min(yValue, 1)
@victorBaro
victorBaro / MagnifyingView.swift
Created September 1, 2018 22:12
A magnifying glass effect view to show on top of screen content as seen in this video: https://www.youtube.com/watch?v=MppiS0iD3qY
public class MagnifyingView: UIView {
private weak var viewToMagnify: UIView!
private var touchPoint: CGPoint!
var zoomScale: CGFloat = 2
public required init?(coder aDecoder: NSCoder) { fatalError() }
public init(viewToMagnify: UIView, size: CGSize) {
self.viewToMagnify = viewToMagnify
super.init(frame: CGRect(origin: .zero, size: size))
@victorBaro
victorBaro / MagnifyingSuperview.swift
Created September 1, 2018 22:40
Control magnifying glass view from another view
class ViewController: UIViewController {
//Set your views and long press recognizer to magnifyControlView
private func handleMagnify(_ recognizer: UILongPressGestureRecognizer) {
let point = recognizer.location(in: magnifyControlView).multiplyValues(by: controlScale)
switch recognizer.state {
case .began:
magnifyView = MagnifyingView(viewToMagnify: comicPageImageView, size: CGSize(width: 200, height: 200))
@victorBaro
victorBaro / PaddingLabel.swift
Created December 6, 2018 18:45
PaddingLabel is a subclass of UILabel that allows you to specify internal padding. Specially useful for labels with background color or/and border.
class PaddingLabel: UILabel {
var padding: UIEdgeInsets {
didSet {
invalidateIntrinsicContentSize()
}
}
public required init(padding: UIEdgeInsets = .zero) {
self.padding = padding
super.init(frame: CGRect.zero)
@victorBaro
victorBaro / ArrayExtensions.swift
Created December 12, 2018 16:17
Array extension for grouping its values
extension Array {
func grouped<T: Equatable>(by value: (Element) -> T) -> [[Element]] {
var result = [[Element]]()
for element in self {
var foundIndex: Int?
for (index, internalArray) in result.enumerated() {
if let firstValue = internalArray.first, value(firstValue) == value(element) {
foundIndex = index
continue
}