【问题标题】:Xcode How do you make circular hitboxes on buttons?Xcode 你如何在按钮上制作圆形命中框?
【发布时间】:2014-09-26 16:38:51
【问题描述】:

我正在使用 Xcode 5 制作 UIButton。我在情节提要中制作了按钮,并将背景图像设置为具有透明度的圆形 .png。我为按钮设置了一个简单的操作,以显示它在标签中被按下的次数。

当我在屏幕上按下圆圈的角时,它仍然会增加分数。所以按钮仍然保持它的方形碰撞框,即使它有一个圆形图像作为它的背景。我到处寻找制作圆形碰撞盒的方法,但找不到任何东西。这甚至可能吗?有没有其他方法可以做到这一点?

【问题讨论】:

标签: xcode button


【解决方案1】:

按钮响应触摸角,因为无论您为背景设置什么图像,按钮的边界框都是一个矩形。

要实现圆形 UIButton,您必须使用以下代码制作圆形边界框:

- (UIView *)setRoundedView:(UIView *)roundedView toDiameter:(float)newSize {
CGPoint saveCenter = roundedView.center;
CGRect newFrame = CGRectMake(roundedView.frame.origin.x, roundedView.frame.origin.y, newSize, newSize);
roundedView.frame = newFrame;
roundedView.layer.cornerRadius = newSize / 2.0;
roundedView.center = saveCenter;
roundedView.layer.masksToBounds = YES;
return roundedView;
}

在此方法中,您可以在您的案例 UIButton 中传递任何 UIView 或 UIView 的子类。所以在代码中你必须为你的按钮创建一个 IBOutlet 并且在你的 -viewDidLoad 方法中简单地输入这行代码:

self.btnMyButton = (UIButton *)[self setRoundedView:self.profilePic toDiameter:34]; 

假设您的 UIButton 插座是 btnMyButton 并根据您的要求设置直径。

希望这会有所帮助。

别忘了导入 QuartzCore 框架。

【讨论】:

    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    相关资源
    最近更新 更多