如何以给定图像的形状创建 UIButton。
我能够创建接近它的东西,但图像似乎不适合按钮。
我的要求是下面给出的图像。即;半圆
下面给出了我用来创建按钮的代码。
我应该对下图进行哪些更改才能获得此按钮。
ps-add subview 在另一个类中完成..
btnStartGame =[UIButton buttonWithType:UIButtonTypeCustom];
[btnStartGame setFrame:CGRectMake(60, 200, 200, 200)];
btnStartGame.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:30];
[btnStartGame setImage:
[UIImage imageNamed:@"Draw button.png"] forState:UIControlStateNormal];
btnStartGame.titleLabel.textColor=[UIColor redColor];
btnStartGame.clipsToBounds = YES;
btnStartGame.layer.cornerRadius = 50;//half of the width
btnStartGame.layer.borderColor=[UIColor redColor].CGColor;
btnStartGame.layer.borderWidth=2.0f;
btnStartGame.tag=20;
btnStartGame.highlighted=NO;
最佳答案
这里有一个非常棒的教程,其中包含可下载的源代码:
http://iphonedevelopment.blogspot.co.uk/2010/03/irregularly-shaped-uibuttons.html
本质上你需要在 UIImage 上创建一个类别,它会检查你触摸的点是否是透明图像。这意味着您可以使用它来检查不规则形状的 HitTest 。
然后将 UIButton 子类化并覆盖 hitTest:withEvent:
希望这可以帮助