Last active
April 14, 2017 13:08
-
-
Save mcomisso/5f769d4d39c66468a362d6af06f07cef to your computer and use it in GitHub Desktop.
A protocol that enables wiggle-wiggle motion to UICollectionViewCells
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
//MARK:- UICollectionViewCells | |
import Material | |
public protocol CellEditable { | |
func deleteElement() | |
func startBeingAfraid() | |
func stopBeingAfraid() | |
} | |
public extension CellEditable where Self: UICollectionViewCell { | |
// | |
// I can't make this button work. addTarget doesn't bind correctly and never gets fired when I tap it. | |
// | |
var removeButton: Button? { | |
get { | |
let tag = 666 | |
if let removeButton = self.contentView.viewWithTag(tag) as? Button { | |
return removeButton | |
} else { | |
let removeButton = Button(frame: CGRect(x: 10, y: 10, width: 25, height: 25)) | |
removeButton.isUserInteractionEnabled = true | |
removeButton.translatesAutoresizingMaskIntoConstraints = false | |
removeButton.layer.cornerRadius = (removeButton.frame.size.width / 2.0) as CGFloat | |
removeButton.clipsToBounds = true | |
removeButton.backgroundColor = SQThemeManager.currentTheme().color.accentDark | |
removeButton.tag = tag | |
removeButton.setImage(Icon.close, for: .normal) | |
removeButton.tintColor = .white | |
return removeButton | |
} | |
} | |
} | |
/// Makes the cell frame wiggle | |
func startBeingAfraid() { | |
let angleLeft = CGFloat((-5.0 * Double.pi) / 180.0) | |
let angleRight = CGFloat((5.0 * Double.pi) / 180.0) | |
if let rmButton = self.removeButton { | |
self.addSubview(rmButton) | |
NSLayoutConstraint.activate([rmButton.topAnchor.constraint(equalTo: self.topAnchor), | |
rmButton.leadingAnchor.constraint(equalTo: self.leadingAnchor), | |
rmButton.widthAnchor.constraint(equalToConstant: 25), | |
rmButton.heightAnchor.constraint(equalToConstant: 25)]) | |
} | |
self.contentView.transform = CGAffineTransform(rotationAngle: angleLeft) | |
UIView.animate(withDuration: 0.15, | |
delay: 0, | |
options: [.allowUserInteraction, .autoreverse, .repeat], | |
animations: { | |
self.contentView.transform = CGAffineTransform(rotationAngle: angleRight) | |
}, completion: nil) | |
} | |
/// Stops the cell frame to wiggle | |
func stopBeingAfraid() { | |
UIView.animate(withDuration: 0.15, | |
delay: 0, | |
options: [.allowUserInteraction, .beginFromCurrentState, .curveEaseOut], | |
animations: { | |
self.contentView.transform = CGAffineTransform.identity | |
}, completion: nil) | |
if let rmButton = self.removeButton { | |
self.removeConstraints(rmButton.constraints) | |
self.removeButton?.removeFromSuperview() | |
self.setNeedsLayout() | |
self.layoutIfNeeded() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment