ios: UI学习总结(user Interface)UIButton

1、UIButton:
1.1、
#pragma  mark 通过代码创建UIButton
-(void)makeUIButtonInSource{
    UIButton *btn = [[UIButton alloc]init]; //初始化UIButton
    btn.frame = CGRectMake(0, 0, 100, 100); //为UIButton指定所在的父控件的位置
    [btn setTitle:@"点我啊" forState:UIControlStateNormal]; //为UIButton 设置在正常状态下的title(text)
    [btn setTitle"@"摸我干啥" forState:UIControlStateHighlighted]; //为UIButton 设置在点击状态下的title(text)
    
    //设置正常状态下的UIButton的title颜色
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];  
    //设置被点击状态下的UIButton的title的颜色
    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    //设置正常状态下的UIButton的背景图片
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_01.png"] forState:(UIControlStateNormal)];
    //设置被点击状态下的UIButton的背景图片
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_02.png"] forState:UIControlStateHighlighted];
    //为UIButton 添加点击事件方法-------通过addTarget的方式
    [btn addTarget: self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    //将UIButton添加到它的父控件上
    [self.view addSubview:btn];
}


#pragma mark 按钮点击事件
-(void)onClick:(UIButton *)btn{
    NSLog(@"摸我干啥!");
}


1.2、


#pragma mark view属性
-(void)onClick:(UIButton *)btn{
    NSLog(@"摸我干啥!");
    
    
    [self setAnimationForImageButton:^{
        //移动按钮
        
        CGRect tempFram = _imageButton.frame;
        tempFram.origin.x -= 100;
        tempFram.origin.y -= 100;
        _imageButton.frame =tempFram;
        
        //    CGPoint tempCenter = _imageButton.center;
        //    tempCenter.x +=100;
        //    tempCenter.y +=100;
        //    _imageButton.center = tempCenter;
        
        //放大缩小按钮
        //    CGRect tempFrame = _imageButton.frame;
        //    tempFrame.size.height -= 10;
        //    tempFrame.size.width -= 10;
        //    _imageButton.frame = tempFrame;
        //大于1为放大,小于为缩小
        //    _imageButton.transform = CGAffineTransformScale(_imageButton.transform, 1.2, 1.2);
        
        //旋转图片
        //    CGFloat angle = -M_PI_4; //正数为逆时针,负数为顺时针
        //    _imageButton.transform = CGAffineTransformRotate(_imageButton.transform, angle);
        
        //将缩放,旋转后的图片恢复到初始状态
        //    _imageButton.transform = CGAffineTransformIdentity;
    }];
    
}


#pragma mark 通过block的方式可以将相同的代码抽取出来进行重构
// void (^block)() =^(){};
-(void)setAnimationForImageButton:(void (^)()) block{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:20];
    block();
    [UIView commitAnimations];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值