Skip to content

Instantly share code, notes, and snippets.

@theoknock
Created January 5, 2022 09:43
Show Gist options
  • Save theoknock/c37457c755d948888aeb050410a3b90d to your computer and use it in GitHub Desktop.
Save theoknock/c37457c755d948888aeb050410a3b90d to your computer and use it in GitHub Desktop.
Passing a block to the CADisplayLink displayLinkWithTarget: parameter. This allows for an animation to be fully contained within a single block (here, animation()), which can be inserted into any object it animates, and that has access to it
void (^(^(^animation)(void))(ControlState))(void) = ^{
static CGFloat starting_radius, ending_radius, radius_step, radius_val;
return ^{
starting_radius = radius;
ending_radius = CGRectGetMinX(control_view.layer.bounds);
radius_step = 0.0005; //(ending_radius - radius);
radius_val = starting_radius;
void (^eventHandlerBlock)(void) = ^{
radius_val += radius_step;
for (CaptureDeviceConfigurationControlProperty property = CaptureDeviceConfigurationControlPropertyTorchLevel; property < CaptureDeviceConfigurationControlPropertyNone; property++) {
[bg(property)() setCenter:[[UIBezierPath bezierPathWithArcCenter:center_point radius:radius_val startAngle:degreesToRadians(CaptureDeviceConfigurationPropertyButtonAngle(property)) endAngle:degreesToRadians(CaptureDeviceConfigurationPropertyButtonAngle(property)) clockwise:FALSE] currentPoint]];
}
if (radius_val >= ending_radius)
{
[display_link invalidate];
[display_link removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
};
const NSUInteger frameInterval = (unsigned int)round(radius_max - radius_min);
return ^ {
[display_link invalidate];
display_link = [CADisplayLink displayLinkWithTarget:eventHandlerBlock selector:@selector(invoke)];
display_link.preferredFramesPerSecond = frameInterval;
[display_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
};
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment