gpt4 book ai didi

iphone - 如何创建圆形按钮?

转载 作者:IT老高 更新时间:2023-10-28 11:47:23 27 4
gpt4 key购买 nike

我想创建一个圆形圆形按钮。这个按钮应该看起来像一个圆圈。

这段代码给出了圆形矩形按钮。

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];

结果

enter image description here

这个概念中的几何

enter image description here

关于iphone - 如何创建圆形按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6952755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com