IOS学习之UIButton

16 篇文章 0 订阅
//
//  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值