我想创建一个圆形按钮。这个按钮应该看起来像一个圆圈。
此代码给出了圆形矩形按钮。
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 30);
UIImage *image = [UIImage imageNamed:@"01.png"];
[button setImage:image forState:UIControlStateNormal];
[image release];
我想出了如何创建一个圆角矩形按钮,但我想创建一个圆形按钮。我需要改变什么?
测试代码:
.h
#import <QuartzCore/QuartzCore.h>
-(void)roundButtonDidTap:(UIButton*)tappedButton;
.m
#define ROUND_BUTTON_WIDTH_HEIGHT YourButtonWidthToBeSetHere
-(void)roundButtonDidTap:(UIButton*)tappedButton{
NSLog(@"roundButtonDidTap Method Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"TimoonPumba.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
//width and height should be same value
button.frame = CGRectMake(0, 0, ROUND_BUTTON_WIDTH_HEIGHT, ROUND_BUTTON_WIDTH_HEIGHT);
//Clip/Clear the other pieces whichever outside the rounded corner
button.clipsToBounds = YES;
//half of the width
button.layer.cornerRadius = ROUND_BUTTON_WIDTH_HEIGHT/2.0f;
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Result
这个概念中的几何
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)