ios 对于按钮的操作

按钮文本长度

我们可以通过titleLabel属性,可以修改其标题,但是如果文本的长度过长显示不下就应该将文本换行展示出来:
    [btn setTitle:@"当我们的文本过长时\n 我们需要换行显示" forState:UIControlStateNormal];
    btn.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
我们处理的方式就是在文本中插入换行字符,这样就能在word wrap模式下实现强制回车了。

按钮添加动画

我们可以将图形放在按钮的前后或者是后面,但是要将图形的交互模式关掉,禁止用户的其他操作干扰到按钮,用户可以看到图形的动画效果。

按钮添加多种状态

在开发的过程中有的时候我们对于按钮有多种状态来满足需求,我们可以通过switch语句选择相关状态下的图案,这样就能够令按钮支持更多的图案和状态了。

按钮以动画效果响应用户

简单事例:
+ (instancetype) button
{
    PushButton *button = [PushButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundColor:[UIColor redColor]];
    [button addTarget:button action:@selector(toggleButton:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:button action:@selector(zoomButton:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragInside | UIControlEventTouchDragEnter ];
    [button addTarget:button action:@selector(relaxButton:) forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel | UIControlEventTouchUpOutside ];
    return button;
}

- (void)toggleButton:(UIButton *)button
{
    [self relaxButton:button];
}

- (void)zoomButton:(UIButton *)button
{
    [UIView animateWithDuration:0.2f animations:^{
        button.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
    }];
}

- (void)relaxButton:(UIButton *)button
{
    [UIView animateWithDuration:0.2f animations:^{
        button.transform = CGAffineTransformIdentity;
    }];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值