Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
KVO on a views geometry
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 300, 200)];
view.backgroundColor = [UIColor blueColor];
[self.view addSubview:view];
[view addObserver:self forKeyPath:@"frame" options:0 context:NULL];
[view.layer addObserver:self forKeyPath:@"bounds" options:0 context:NULL];
[view.layer addObserver:self forKeyPath:@"transform" options:0 context:NULL];
[view.layer addObserver:self forKeyPath:@"position" options:0 context:NULL];
[view.layer addObserver:self forKeyPath:@"zPosition" options:0 context:NULL];
[view.layer addObserver:self forKeyPath:@"anchorPoint" options:0 context:NULL];
[view.layer addObserver:self forKeyPath:@"anchorPointZ" options:0 context:NULL];
[view.layer addObserver:self forKeyPath:@"frame" options:0 context:NULL];
[view.layer addObserver:self forKeyPath:@"transform" options:0 context:NULL];
[self doSomeRandomAlterationToViewRepeated:view];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"View changed its geometry");
}
- (void)doSomeRandomAlterationToViewRepeated:(UIView *)view
{
double delayInSeconds = 0.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self doSomeRandomAlterationToView:view];
[self doSomeRandomAlterationToViewRepeated:view];
});
}
- (void)doSomeRandomAlterationToView:(UIView *)view
{
switch (arc4random_uniform(9)) {
case 0:
{
NSLog(@"Changing frame");
CGRect frame = view.frame;
frame.size.width += (float)arc4random_uniform(100);
view.frame = frame;
}
break;
case 1:
{
NSLog(@"Changing bounds");
CGRect bounds = view.bounds;
bounds.size.width = (float)arc4random_uniform(100);
view.frame = bounds;
}
break;
case 2:
{
NSLog(@"Changing center");
CGPoint center = view.center;
center.x = (float)arc4random_uniform(100);
view.center = center;
}
break;
case 3:
{
NSLog(@"Changing layer.anchorPoint");
CGPoint anchor = view.layer.anchorPoint;
anchor.x = ((float)arc4random_uniform(5) / 10.0);
view.layer.anchorPoint = anchor;
}
break;
case 4:
{
NSLog(@"Changing layer.position");
CGPoint position = view.layer.position;
position.x = ((float)arc4random_uniform(5) / 10.0);
view.layer.position = position;
}
break;
case 5:
{
NSLog(@"Changing layer.anchorPointZ");
CGFloat anchor = view.layer.anchorPointZ;
anchor = (float)arc4random_uniform(20);
view.layer.anchorPointZ = anchor;
}
break;
case 6:
{
NSLog(@"Changing layer.bounds");
CGRect bounds = view.layer.bounds;
bounds.size.width = (float)arc4random_uniform(100);
view.layer.bounds = bounds;
}
break;
case 7:
{
NSLog(@"Changing layer.transform");
CATransform3D transform = view.layer.transform;
transform.m32 = (float)arc4random_uniform(100);
view.layer.transform = transform;
}
break;
case 8:
{
NSLog(@"Changing transform");
CGAffineTransform transform = view.transform;
transform.a = ((float)arc4random_uniform(5) / 10.0);
view.transform = transform;
}
break;
default:
break;
}
}
@pedro380085

This comment has been minimized.

Copy link

pedro380085 commented Feb 21, 2014

Why [view.layer addObserver:self forKeyPath:@"zPosition" options:0 context:NULL]; exists twice?

@hfossli

This comment has been minimized.

Copy link
Owner Author

hfossli commented Sep 16, 2014

@pedro380085 typo :) (fixed now)

@onmyway133

This comment has been minimized.

Copy link

onmyway133 commented Jul 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.