本文主要是介绍Snail—UI学习之UIButton,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
UIButton的一些简单应用 ,现在写的都是一些自己用到的遇到的,一些其他的目前还没有看
#import "WJJRootViewController.h"@interface WJJRootViewController ()@end@implementation WJJRootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {// Custom initialization}return self;
}//当前界面的主入口 只调用一次
- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor = [UIColor whiteColor];/*UIButtonTypeContactAdd 带一个加号标志的按钮UIButtonTypeCustom 自定义按钮 UIButtonTypeDetailDisclosure 就是按钮上一个圆圈 里面一个i 类似于某事物的细节按钮UIButtonTypeRoundedRect 圆角按钮UIButtonTypeSystem 系统按钮样式*/UIButton * button1 = [UIButton buttonWithType:UIButtonTypeSystem];//设置按钮的位置大小button1.frame = CGRectMake(20, 20, 280, 30);//设置按钮的背景颜色button1.backgroundColor = [UIColor blackColor];//设置按钮的标题 及当前按钮的状态为一般状态(未点击状态)/*forState:UIControlStateNormal:正常状态 未被点击UIControlStateHighlighted 高亮状态UIControlStateDisabled 禁用状态UIControlStateSelected 选中状态UIControlStateApplicationUIControlStateReserved 保留状态*/[button1 setTitle:@"点我1" forState:UIControlStateNormal];//设置其标记位button1.tag = 1;//设置按钮是否可按动 YES:可以 NO:不可用button1.enabled = YES;//为按钮添加一个事件/*第一个参数:self指本视图控制器第二个参数:按下按钮后要执行什么操作第三个参数:UIControlEventTouchUpInside 表示当按钮按下并且鼠标抬起的瞬间会触发操作UIControlEventTouchDown:按下去未抬起鼠标时将触发操作UIControlEventTouchDownRepeat 鼠标连续按下多次时将触发下面几个每次操作时,都会有按钮被按下去的操作UIControlEventTouchDragInside 按下按钮,并在此空间范围内拖动时UIControlEventTouchDragOutside 按钮按钮,在此空间范围外拖动*/[button1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:button1];//再新建一个ButtonUIButton * button2 = [UIButton buttonWithType:UIButtonTypeSystem];button2.frame = CGRectMake(20, 60, 280, 30);button2.tag = 2;button2.backgroundColor = [UIColor redColor];[button2 setTitle:@"点我2" forState:UIControlStateNormal];//此action中得click:是代表有参数的click方法[button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:button2];//自定义按钮 UIButtonTypeCustomUIButton * button3 = [UIButton buttonWithType:UIButtonTypeCustom];button3.frame = CGRectMake(20, 100, 31, 30);[button3 setTitle:@"点我3" forState:UIControlStateNormal];button3.tag = 3;//设置button的初始状态 已经选择的状态button3.selected = NO;//设置自定义按钮两种状态下得不同图片//未选中状态显示的是第一个图片 选中状态后是后边的一个图片[button3 setImage:[UIImage imageNamed:@"star_icon@2x.png"] forState:UIControlStateNormal];[button3 setImage:[UIImage imageNamed:@"star2_Gray@2x.png"] forState:UIControlStateSelected];[button3 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:button3];}//按下按钮后,将触发的操作写在这里
- (void)click{NSLog(@"你点我了1");
}
//如果有多个按钮要触发同一个操作,但是又想实现不同按钮方法将执行不同操作时,就要判断一下是哪个按钮按下了
- (void)click:(UIButton *)button{NSLog(@"你点我了2");
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
运行效果图
这篇关于Snail—UI学习之UIButton的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!