Skip to content

Instantly share code, notes, and snippets.

@BradB132
BradB132 / AutoLayoutController.swift
Last active August 29, 2015 14:22
AutoLayoutRecipesPt2-1
@IBOutlet weak var constraint1: NSLayoutConstraint!
@IBOutlet weak var constraint2: NSLayoutConstraint!
@IBAction func slider1Changed(sender: AnyObject) {
constraint1.constant = CGFloat((sender as UISlider).value)
}
@IBAction func slider2Changed(sender: AnyObject) {
constraint2.constant = CGFloat((sender as UISlider).value)
}
@BradB132
BradB132 / AutoLayoutController.swift
Last active August 29, 2015 14:22
AutoLayoutRecipesPt2-2
@IBOutlet weak var removableView: UIView!
@IBAction func removeView(sender: AnyObject) {
removableView.removeFromSuperview()
}
@BradB132
BradB132 / AutoLayoutController.swift
Created May 28, 2015 22:20
AutoLayoutRecipesPt1-2
@IBOutlet weak var constraint1: NSLayoutConstraint!
@IBAction func slider1Changed(sender: AnyObject) {
constraint1.constant = CGFloat((sender as UISlider).value)
}
@BradB132
BradB132 / AutoLayoutController.swift
Created May 28, 2015 22:20
AutoLayoutRecipesPt1-1
@IBOutlet weak var constraint1: NSLayoutConstraint!
@IBOutlet weak var constraint2: NSLayoutConstraint!
@IBAction func animateViews(sender: AnyObject) {
constraint1.constant = CGFloat(arc4random_uniform(200))
constraint2.constant = CGFloat(arc4random_uniform(200))
UIView.animateWithDuration(0.5, animations: {
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
@BradB132
BradB132 / CustomLayout.m
Last active August 29, 2015 14:22
GettingStartedWithCustomUICollectionViewLayouts-1
-(void)prepareLayout
{
//don't forget to call super here
[super prepareLayout];
/* pre-calculate and cache whatever you need here */
//maybe you would want to loop over each cell like this?
for(NSUInteger i = 0; i < [self.collectionView numberOfSections]; i++)
{
@BradB132
BradB132 / CustomLayout.m
Last active August 29, 2015 14:22
GettingStartedWithCustomUICollectionViewLayouts-2
- (CGSize)collectionViewContentSize
{
return CGSizeMake(/* calculate your width */,
/* calculate your height */);
}
@BradB132
BradB132 / CustomLayout.m
Last active August 29, 2015 14:22
GettingStartedWithCustomUICollectionViewLayouts-3
-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray* elementsInRect = [NSMutableArray array];
//iterate over all cells in this collection
for(NSUInteger i = 0; i < [self.collectionView numberOfSections]; i++)
{
for(NSUInteger j = 0; j < [self.collectionView numberOfItemsInSection:i]; j++)
{
//this is the cell at row j in section i
@BradB132
BradB132 / CustomLayout.m
Last active August 29, 2015 14:22
GettingStartedWithCustomUICollectionViewLayouts-4
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGRect cellFrame = CGRectMake(/* calculate your origin x */,
/* calculate your origin y */,
/* calculate your width */,
/* calculate your height */);
attr.frame = cellFrame;
@BradB132
BradB132 / GemsOfSwift.swift
Created May 31, 2015 14:36
FirstAppInSwift-1
class MyClass {
lazy var myLazyProperty = ExpensiveObject()
}
@BradB132
BradB132 / GemsOfSwift.swift
Created May 31, 2015 14:37
FirstAppInSwift-2
func exampleFunc(intArg:Int) {
// ...
}
exampleFunc(5) //just pass the expression, no name needed