将矩形按钮框剪切为图像蒙版以与UIDynamics一起使用

时间:2014-02-07 15:07:09

标签: ios objective-c uibutton uikit-dynamics

我的屏幕上有三个按钮,在故事板中设置了背景图像,背景图像是六边形。我正在玩重力,当按下按钮时,我希望它们都落到屏幕的底部。我希望按钮能够反应,就像它们是相互反弹时的六边形形状而不是它们的矩形形状。

有没有办法将UIButtons.frame剪辑为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];
}

我试过谷歌搜索无济于事。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

UIKit Dynamics仅支持此协议定义的矩形形状。

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

也许SpriteKit的SKPhysicsBody适合你的情况。具体来说,将hexagon path传递给此初始化程序。

+ (SKPhysicsBody *)bodyWithPolygonFromPath:(CGPathRef)path