Skip to content

Instantly share code, notes, and snippets.

@Ciechan
Created May 22, 2014 14:54
Show Gist options
  • Save Ciechan/09cc466788fd99c7850a to your computer and use it in GitHub Desktop.
Save Ciechan/09cc466788fd99c7850a to your computer and use it in GitHub Desktop.
BCMeshTransform left curtain
/*
BCMutableMeshTransform+DemoTransforms.m
*/
+ (instancetype)curtainMeshTransformAtPoint:(CGPoint)point boundsSize:(CGSize)boundsSize
{
const float Frills = 3;
point.x = MIN(point.x, boundsSize.width);
BCMutableMeshTransform *transform = [BCMutableMeshTransform identityMeshTransformWithNumberOfRows:20 numberOfColumns:50];
CGPoint np = CGPointMake(1.0 - point.x/boundsSize.width, point.y/boundsSize.height);
[transform mapVerticesUsingBlock:^BCMeshVertex(BCMeshVertex vertex, NSUInteger vertexIndex) {
float dy = vertex.to.y - np.y;
float bend = 0.25f * (1.0f - expf(-dy * dy * 10.0f));
float x = 1.0 - vertex.to.x;
vertex.to.z = 0.1 + 0.1f * sin(-1.4f * cos(x * x * Frills * 2.0 * M_PI)) * (1.0 - np.x);
vertex.to.x = 1.0 - (x * np.x + x * bend * (1.0 - np.x));
return vertex;
}];
return transform;
}
/*
BCCurtainDemoViewController.m
*/
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self.transformView];
return point.x < 30.0;
}
- (void)pan:(UIPanGestureRecognizer *)sender
{
CGPoint point = [sender locationInView:self.transformView];
switch (sender.state) {
case UIGestureRecognizerStateBegan:
self.surplus = point.x;
// FALL THROUGH
case UIGestureRecognizerStateChanged:
self.transformView.meshTransform = [BCMutableMeshTransform curtainMeshTransformAtPoint:CGPointMake(point.x - self.surplus, point.y)
boundsSize:self.transformView.bounds.size];
break;
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
{
CGFloat percent = 1.0 - point.x/self.transformView.bounds.size.width;
NSTimeInterval duration = 0.1 + 0.5 * percent;
[UIView animateWithDuration:duration animations:^{
[self setIdleTransform];
}];
break;
}
default:
break;
}
}
- (void)setIdleTransform
{
self.transformView.meshTransform = [BCMutableMeshTransform curtainMeshTransformAtPoint:CGPointMake(0, self.view.bounds.size.height/2.0)
boundsSize:self.transformView.bounds.size];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment