Clipping rectangular button frame to image mask for use with UIDynamics

Nick Langley

I have three buttons on my screen which have background images set in storyboard, the background images are hexagonal shapes. I'm currently playing around with gravity, when a button is pressed I want them all to fall to the bottom of the screen. I would like the buttons to react like they are hexagonal shapes when bouncing off each other rather than the rectangular shapes they are.

Is there a way to clip the UIButtons.frame to the hexagonal.png?

- (IBAction)home:(id)sender {

    animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    gravity = [[UIGravityBehavior alloc] initWithItems:@[self.youtuberLyr, self.gameLyr, self.homeLyr]];
    [animator addBehavior:gravity];

    collision = [[UICollisionBehavior alloc] initWithItems:@[self.youtuberLyr, self.gameLyr, self.homeLyr]];
    collision.translatesReferenceBoundsIntoBoundary = YES;
    [animator addBehavior:collision];

    barrier = [[UIView alloc] initWithFrame:CGRectMake(0, 1024, 768, 0)];
    barrier.backgroundColor = [UIColor redColor];
    [self.view addSubview:barrier];

    CGPoint rightEdge = CGPointMake(barrier.frame.origin.x + barrier.frame.size.width, barrier.frame.origin.y);
    [collision addBoundaryWithIdentifier:@"barrier" fromPoint:barrier.frame.origin toPoint:rightEdge];
}

I have tried googling to no avail. Any help is greatly appreciated.

Jason Harwig

UIKit Dynamics only supports rectangle shapes as defined by this protocol.

@protocol UIDynamicItem <NSObject>
@property (nonatomic, readwrite) CGPoint center;
@property (nonatomic, readonly) CGRect bounds;
@property (nonatomic, readwrite) CGAffineTransform transform;    
@end

Maybe SpriteKit's SKPhysicsBody would work for your case. Specifically, by passing a hexagon path to this initializer.

+ (SKPhysicsBody *)bodyWithPolygonFromPath:(CGPathRef)path

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related