Last active
August 29, 2015 14:06
-
-
Save kyleturner/35e5c438e9b8a474b756 to your computer and use it in GitHub Desktop.
Beam iOS Collection View Constraints Animation Adjustments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)adjustConstraintsAndApplyAnimationsForCell:(NGContactCollectionViewCell *)cell | |
{ | |
CGFloat imageWidth = 104.0; | |
CGFloat imageHeight = 104.0; | |
CGFloat defaultActionImageHeight = (self.editing) ? 18.0 : 27.0; | |
CGFloat nameLabelXOffset = (self.editing) ? 5.0 : 4.0; | |
CGFloat nameFontSize = (self.editing) ? 12.0 : 16.0; | |
BOOL contactHasImage = cell.contact.hasContactImage; | |
if (self.editing) { | |
imageWidth = (contactHasImage) ? 62.0 : 50.0; | |
imageHeight = (contactHasImage) ? 62.0 : 50.0; | |
} | |
NoArgumentBlock adjustConstraintsForDefaultImageCells = ^{ | |
if (!contactHasImage) { | |
// adjust default avatar image size (make room for name label) | |
[cell.defaultActionImageView.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) { | |
if (constraint.firstAttribute == NSLayoutAttributeHeight) { | |
constraint.constant = defaultActionImageHeight; | |
} | |
if (constraint.firstAttribute == NSLayoutAttributeWidth) { | |
constraint.constant = defaultActionImageHeight; | |
} | |
}]; | |
// adjust name label position and text alignment | |
[cell.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) { | |
if (constraint.firstItem == cell.nameLabel && constraint.secondItem == cell && constraint.firstAttribute == NSLayoutAttributeLeading) { | |
constraint.constant = nameLabelXOffset; | |
} | |
}]; | |
cell.nameLabel.font = [UIFont fontWithName:cell.nameLabel.font.fontName size:nameFontSize]; | |
cell.nameLabel.textAlignment = (self.editing) ? NSTextAlignmentCenter : NSTextAlignmentLeft; | |
[cell.contentView layoutIfNeeded]; | |
} | |
}; | |
NoArgumentBlock adjustConstraints = ^{ | |
[UIView animateWithDuration:0.15 animations:^{ | |
// adjust image size | |
[cell.contactImageView.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) { | |
if (constraint.firstAttribute == NSLayoutAttributeWidth) { | |
constraint.constant = imageWidth; | |
} | |
if (constraint.firstAttribute == NSLayoutAttributeHeight) { | |
constraint.constant = imageHeight; | |
} | |
}]; | |
// adjust default action size | |
[cell.defaultActionImageView.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) { | |
if (constraint.firstAttribute == NSLayoutAttributeHeight) { | |
constraint.constant = defaultActionImageHeight; | |
} | |
if (constraint.firstAttribute == NSLayoutAttributeWidth) { | |
constraint.constant = defaultActionImageHeight; | |
} | |
}]; | |
[cell.contentView layoutIfNeeded]; | |
}]; | |
}; | |
adjustConstraintsForDefaultImageCells(); | |
// toggle animations for editing state | |
if (self.editing) { | |
[UIView animateWithDuration:0.15 animations:^{ | |
cell.closeButton.hidden = NO; | |
adjustConstraintsForDefaultImageCells(); | |
adjustConstraints(); | |
[self startWobbleForCell:cell]; | |
}]; | |
} | |
else { | |
[UIView animateWithDuration:0.15 animations:^{ | |
cell.closeButton.hidden = YES; | |
adjustConstraints(); | |
[self stopWobbleForCell:cell]; | |
}]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you thought about adding a
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
method toNGContactCollectionViewCell
? Some advantages to that:UICollectionViewDataSource
NSLayoutConstraints
that you care about, to avoid needing to enumerate them every time. (These can even beIBOutlets
if you design the cell in IB)imageWidth
andimageHeight
, into the cell class.cell.contactImageView
,cell.closeButton
, etc.)