Skip to content

Instantly share code, notes, and snippets.

@kyleturner
Last active August 29, 2015 14:06
Show Gist options
  • Save kyleturner/35e5c438e9b8a474b756 to your computer and use it in GitHub Desktop.
Save kyleturner/35e5c438e9b8a474b756 to your computer and use it in GitHub Desktop.
Beam iOS Collection View Constraints Animation Adjustments
- (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];
}];
}
}
@justinvoss
Copy link

Have you thought about adding a -(void)setEditing:(BOOL)editing animated:(BOOL)animated method to NGContactCollectionViewCell? Some advantages to that:

  • If the visual style of the cell changes, then you only need to update the cell class, instead of also updating the UICollectionViewDataSource
  • You could add some private properties on that cell class to keep references to the NSLayoutConstraints that you care about, to avoid needing to enumerate them every time. (These can even be IBOutlets if you design the cell in IB)
  • You could move some of those "magic numbers," like imageWidth and imageHeight, into the cell class.
  • The data source no longer needs to know about the internal view hierarchy of cells (no more reaching into cell.contactImageView, cell.closeButton, etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment