关闭

iOS -- UI常用组件之. Button

标签: iosuiuibuttonclass
74人阅读 评论(0) 收藏 举报

iOS – UI常用组件之. Button

// 常用设置

UIButton *roundButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];//类型
[roundButton setFrame:CGRectMake(60, 50, 200, 40)]; //大小
[roundButton setTitle:@"Round Button" forState:UIControlStateNormal];//名称

// 当buttonType为UIButtonTypeCustom时,

[customButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[customButton setBackgroundImage:[[UIImage imageNamed:@"whiteButton.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:20] forState:UIControlStateNormal]; //图片先拉伸再设置背景
[customButton setBackgroundImage:[[UIImage imageNamed:@"back.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:20] forState:UIControlStateHighlighted];

// 封装Button(实例化、背景图片、弹簧动画的阻尼值、弹簧动画的速率)

//
UIButton * btn1 = [self btnAnimateWithFrame:CGRectMake(LSCREENW/4-30, LSCREENH-80, 60, 60) imageName:@"img_wechat_logo" animateFrame:CGRectMake(LSCREENW/4-30, LSCREENH-130, 60, 60) delay:0.1];
        btn1.tag=1;
        [btn1 addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];

//
-(UIButton *)btnAnimateWithFrame:(CGRect)frame imageName:(NSString *)imageName animateFrame:(CGRect)aniFrame delay:(CGFloat)delay
{
    UIButton * btn =[[UIButton alloc]init];
    btn.frame =frame;
    [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [self  addSubview:btn];

    [UIView animateWithDuration:1 delay:delay usingSpringWithDamping:0.3 initialSpringVelocity:0.2 options:UIViewAnimationOptionAllowUserInteraction animations:^{

        btn.frame  = aniFrame;

    } completion:^(BOOL finished) {

    }];
    return btn;

    //usingSpringWithDamping :弹簧动画的阻尼值,也就是相当于摩擦力的大小,该属性的值从0.0到1.0之间,越靠近0,阻尼越小,弹动的幅度越大,反之阻尼越大,弹动的幅度越小,如果大道一定程度,会出现弹不动的情况。
    //initialSpringVelocity :弹簧动画的速率,或者说是动力。值越小弹簧的动力越小,弹簧拉伸的幅度越小,反之动力越大,弹簧拉伸的幅度越大。这里需要注意的是,如果设置为0,表示忽略该属性,由动画持续时间和阻尼计算动画的效果。

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2423次
    • 积分:109
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:0篇
    • 译文:1篇
    • 评论:0条