UIButton

作者: L柠_檬 | 来源:发表于2016-08-19 12:48 被阅读43次
目录
    1.1 单选逻辑
    1.2 多选逻辑
    1.3 点击放大
1.1 单选

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong)UIButton *lastButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    for (int i = 0; i < 3; i++){
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
        button.frame = CGRectMake(100+i*(50+20) , 200, 50, 20);
    
        [button setTitle:@"单选" forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.tag = i;
    
      [self.view addSubview:button];
    
    }
}


//点击事件
- (void)buttonClicked:(UIButton *)button{
    
    if (button != self.lastButton) {
        
        switch (button.tag) {
                
            case 0:
                
                NSLog(@"0");
                
                break;
                
            case 1:
                
                NSLog(@"1");
                
                break;
                
        }
        
    }
    
   // 设置颜色改变
    
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    [self.lastButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    self.lastButton = button;
    
}
@end
1.2 多选

#import "ViewController.h"

@interface ViewController ()

//定义一个保存点击的字典
@property (nonatomic, strong)NSMutableDictionary *mainDictionary;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.mainDictionary = [[NSMutableDictionary alloc]init];
    
    for (int i = 0; i < 3 ; i++) {
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        
        button.frame = CGRectMake(100+i*(50+20) , 200, 50, 20);
        
        [button setTitle:@"多选" forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(buttonClicked:)
         forControlEvents:UIControlEventTouchUpInside];
        
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
        button.tag = i;
        
        [self.view addSubview:button];
        
    }
    
}


//点击事件

- (void)buttonClicked:(UIButton *)button{
    
    
    if ([[self.mainDictionary allKeys]containsObject:@(button.tag)]) {
        
        [self.mainDictionary removeObjectForKey:@(button.tag)];
        
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        
    }else{
        
        [self.mainDictionary setObject:@(button.tag) forKey:@(button.tag)];
        
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
    }
    
    NSLog(@"%@",self.mainDictionary);
    
}
@end
1.3 点击放大特效

#import Foundation/Foundation.h(这里需要加尖括号)

#import UIKit/UIKit.h

@interface JDButtton : NSObject

/**
 * function: view先缩小,然后放大动画(收藏和点赞)
 * param: 如果无法在NSObject里写UIButton,缺少库
 *
 */

+ (void)buttonAnimation:(UIButton*)button;

@end



#import "JDButtton.h"

@implementation JDButtton

+ (void)buttonAnimation:(UIButton*)button{
    
    CAKeyframeAnimation * animation;
    
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.delegate = self;
    
    animation.duration = 0.2;
    
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.5, 1.0)]];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    
    [button.layer addAnimation:animation forKey:nil];
    
    CAKeyframeAnimation * animation;
    
}

@end

相关文章

网友评论

      本文标题:UIButton

      本文链接:https://www.haomeiwen.com/subject/qgjbsttx.html