关闭

iOS控件之UIButton

345人阅读 评论(0) 收藏 举报
分类:

按钮控件  UIButton  


一,作用:响应用户的点击事件

 二,常用属性

// 设置指定状态对应的标题文本
- (void)setTitle:(NSString *)title forState:(UIControlState)state;


// 设置指定状态对应的标题颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;


// 设置指定状态对应的显示图片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;


// 设置指定状态对应的背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;


// 为按钮添加事件
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;




三,UIButton的状态

UIControlStateNormal  // 正常状态


UIControlStateHighlighted// 高亮状态


UIControlStateDisabled// 禁用状态


UIControlStateSelected// 选中状态

四,UIButtonDemo

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 构造方法
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    /*
    // 禁用按钮
    button.enabled = NO;
    // 按钮选中
    button.selected = YES;
    // 设置标题,状态正常
    [button setTitle:@"normal" forState:UIControlStateNormal];
    // 设置标题,状态高亮
    [button setTitle:@"highlighted" forState:UIControlStateHighlighted];
    // 设置标题,状态禁用
    [button setTitle:@"disabled" forState:UIControlStateDisabled];
    // 设置标题,状态选中
    [button setTitle:@"selected" forState:UIControlStateSelected];
    // 设置title的颜色
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    */
     
    // 设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateSelected];
    
    // 设置图片
//    [button setImage:[UIImage imageNamed:@"love_normal"] forState:UIControlStateNormal];
//    [button setImage:[UIImage imageNamed:@"love_select"] forState:UIControlStateSelected];
    
    // 用户在控件内按下抬起时
    [button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    // 用户按下时
    [button addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchDown];
    // 用户按下时
    [button addTarget:self action:@selector(outside) forControlEvents:UIControlEventTouchUpOutside];
    // 记录用户多次按下
    [button addTarget:self action:@selector(repeat) forControlEvents:UIControlEventTouchDownRepeat];
    // 用户由内向外
    [button addTarget:self action:@selector(dragExit) forControlEvents:UIControlEventTouchDragExit];
    // 用户由外向内
    [button addTarget:self action:@selector(dragEnter) forControlEvents:UIControlEventTouchDragEnter];
    // 事件的取消 如用户刚点击按钮 来电话了
    [button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchCancel];
    
    // 设置frame,与按钮的类型有关
    button.frame = CGRectMake(90, 100, 41, 30);
    [self.window addSubview:button];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)touchUpInside:(UIButton *)button
{
    if (button.selected) {
        button.selected = NO;
    }else {
        button.selected = YES;
    }
    NSLog(@"touchUpInside");
}

- (void)down
{
    NSLog(@"down");
}

- (void)outside
{
    NSLog(@"outside");
}

- (void)repeat
{
    NSLog(@"repeat");
}

- (void)dragExit
{
    NSLog(@"dragExit");
}

- (void)dragEnter
{
    NSLog(@"dragEnter");
}

- (void)cancel
{
    NSLog(@"cancel");
}



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62908次
    • 积分:1584
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:31篇
    • 译文:1篇
    • 评论:3条
    博客专栏
    最新评论