Skip to content

Instantly share code, notes, and snippets.

@joshaber
Created October 12, 2012 19:35
Show Gist options
  • Save joshaber/3881052 to your computer and use it in GitHub Desktop.
Save joshaber/3881052 to your computer and use it in GitHub Desktop.
// Here, rowView is a weak pointer to the view's superview.
RAC(self.layer.contents) = [[[[RACAble(self.rowView.selected) distinctUntilChanged] injectObjectWeakly:self]
doNext:^(RACTuple *x) {
CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"contents"];
fade.duration = 0.15;
[[x.last layer] addAnimation:fade forKey:@"animateContents"];
}] select:^id(RACTuple *x) {
return ([x.first boolValue] ? [self highlightedGradient] : [self standardGradient]);
}];
// So we want our KVO observation to stop when `rowView` is dealloc'd. But note
// that RACAble(self.rowView.selected) is really:
//
// [self rac_subscribableForKeyPath:@"self.rowView.selected" onObject:self]
//
// No where in that can RAC determine that the KVO observation should be tied
// to `rowView`. It's just a key path relative to self. So we really want:
//
// [self.rowView rac_subscribableForKeyPath:@"selected" onObject:self]
//
// That way RAC knows to keep the KVO observation around only as long as self
// and `rowView` are alive. We can then write that as:
//
// RACAble(self.rowView, selected)
//
// This is less than ideal because now whenever `rowView` changes, you have to
// re-subscribe. It's a pain in the ass that we run into too but I don't have a
// great way to solve it. (This is what happens with a language without garbage
// collection and poor static introspection.)
//
// Actually @jspahrsummers' block craziness could probably solve this by
// knowing the root object for the keypath...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment