Skip to content

Instantly share code, notes, and snippets.

@hfossli
Last active April 19, 2024 10:09
Show Gist options
  • Save hfossli/7234623 to your computer and use it in GitHub Desktop.
Save hfossli/7234623 to your computer and use it in GitHub Desktop.
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;
}
}
@onmyway133
Copy link

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