如何创建一个圆形按钮? [英] how to create a round button?

查看:95
本文介绍了如何创建一个圆形按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Iphone开发的新手。

I am new to Iphone development.

我想创建一个圆形的手机按钮。此按钮应该看起来像一个圆圈。

I want to create a round circlular button. This button should look like a circle.

这会给出圆形矩形按钮。

This gives round rectangular button.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 30);
UIImage *img = [UIImage imageNamed:@"01.png"];
[button setImage:img forState:UIControlStateNormal];
[img release];    

我想出了如何创建圆角矩形按钮,但我想创建一个圆形按钮。请建议。

I figured out how to create a rounded rectangular button but I want to create a round circle button. Please suggest.

推荐答案

经过测试的代码:

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

结果

此概念中的几何

这篇关于如何创建一个圆形按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆