//
// UIBottonController.m
// UIButton
//
// Created by zhukui on 16/10/27.
// Copyright © 2016年 zhukui. All rights reserved.
//
#import "UIBottonController.h"
@interface UIBottonController ()
@end
@implementation UIBottonController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *text=@"按钮";
//创建按钮
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
//UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(25, 25, 80, 40)];
//设置frame
button.frame=CGRectMake(25, 25, 65, 29);
//设置背景
button.backgroundColor=[UIColor yellowColor];
//设置文字
[button setTitle:@"" forState:UIControlStateNormal];
//设置button填充图片
[button setBackgroundImage:[UIImage imageNamed:@"btn_payicon@2x.png"] forState:UIControlStateNormal];
//设置文字颜色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
/* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
// enum {
// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 << 0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管
// };
/*
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
* 那么可以去掉这个功能
*/
button.adjustsImageWhenHighlighted = NO;
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
button.adjustsImageWhenDisabled = NO;
/* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
button.showsTouchWhenHighlighted = NO;
//button点击事件
[button addTarget:self action:@selector(btnShort:) forControlEvents:UIControlEventTouchUpInside];
//button长按事件
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(btnLong:)];
longPress.minimumPressDuration=0.5;//默认0.5秒
[button addGestureRecognizer:longPress];
[self.view addSubview:button];
}
-(void)btnShort:(UIButton *)sender{
NSLog(@"点击事件");
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按钮点击提示" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[myAlertView show];
}
-(void)btnLong:(UILongPressGestureRecognizer*)gestureRecognizer{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
NSLog(@"长按事件");
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按钮长按提示" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alter show];
}
}
@end
IOS学习之UIButton
最新推荐文章于 2019-07-20 16:24:50 发布