目錄
1.1 單選邏輯
1.2 多選邏輯
1.3 點(diǎn)擊放大
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];
}
}
//點(diǎn)擊事件
- (void)buttonClicked:(UIButton *)button{
if (button != self.lastButton) {
switch (button.tag) {
case 0:
NSLog(@"0");
break;
case 1:
NSLog(@"1");
break;
}
}
// 設(shè)置顏色改變
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[self.lastButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.lastButton = button;
}
@end
1.2 多選
#import "ViewController.h"
@interface ViewController ()
//定義一個(gè)保存點(diǎn)擊的字典
@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];
}
}
//點(diǎn)擊事件
- (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 點(diǎn)擊放大特效
#import Foundation/Foundation.h(這里需要加尖括號(hào))
#import UIKit/UIKit.h
@interface JDButtton : NSObject
/**
* function: view先縮小月培,然后放大動(dòng)畫(收藏和點(diǎn)贊)
* param: 如果無法在NSObject里寫UIButton抬伺,缺少庫(kù)
*
*/
+ (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
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者