宽容他人,放过自己。

创建连续的有编辑状态的按钮

Posted on By anchoriteFili

按钮来回晃动的动画.zip
CABasicAnimation使用总结

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIView *backView; //背景view


@property (weak, nonatomic) IBOutlet UIButton *animationButton; //动的按钮

@property (weak, nonatomic) IBOutlet UIButton *animationButtonOne; //第二个按钮

@property (nonatomic,strong) NSMutableArray *buttonsArray; //按钮的数组

@property (nonatomic,assign) BOOL isEdit; //设置按钮是否是编辑状态


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.buttonsArray = [NSMutableArray arrayWithObjects:@"北京",@"山东",@"上海",@"南京",@"深圳",@"山海经", nil];
    
    [self createButtonsWithButtonsArray:self.buttonsArray];
    
}

#pragma mark 在buttonsView中循环创建多个button方法
- (void)createButtonsWithButtonsArray:(NSMutableArray *)buttonsArray {
    
    CGFloat width = 0;
    CGFloat y = 0;
    
    for (int i = 0; i < buttonsArray.count; i ++) {
        
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor orangeColor];
        view.frame = CGRectMake( 15+width, y, 74, 35);
        [self.backView addSubview:view];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake( 0, 0, GETWIDTH(view), GETHEIGHT(view));
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = 1000+i;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:[buttonsArray objectAtIndex:i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonsClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"福利灰背景"] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor redColor];
        [view addSubview:button];
        
        
#pragma mark 创建右上角小×按钮
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        imageView.center = CGPointMake(GETWIDTH(view), 0);
        imageView.image = [UIImage imageNamed:@"hot_close"];
        imageView.hidden = YES;
        [view addSubview:imageView];
        
        width = view.frame.size.width+view.frame.origin.x;
        
        if (width >[UIScreen mainScreen].bounds.size.width) {
            width = 0;
            y = view.frame.origin.y+view.frame.size.height+12;
            view.frame = CGRectMake( 15+width, y, 74, 35);
            width = view.frame.size.width+view.frame.origin.x;
        }
    }
}

#pragma mark 下面一堆按钮的点击事件
- (void)buttonsClick:(UIButton *)sender {
    
    for (UIView *view in self.backView.subviews) {
        [view removeFromSuperview];
    }
    
    [self.buttonsArray removeObjectAtIndex:sender.tag-1000];
    [self createButtonsWithButtonsArray:self.buttonsArray];
    self.isEdit = YES;
}

#pragma mark 编辑按钮点击事件
- (IBAction)editBtnClick:(UIButton *)sender {
    
    self.isEdit = !self.isEdit;
}

- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    if (isEdit) {
        
        for (UIView *view in self.backView.subviews) {
            [view.layer addAnimation:[self rotationAnimation] forKey:@"animateLayer"];
            view.subviews.lastObject.hidden = NO;
        }
        NSLog(@"动画开始");
    } else {
        
        for (UIView *view in self.backView.subviews) {
            [view.layer removeAllAnimations];
            view.subviews.lastObject.hidden = YES;
        }
    }
}

#pragma mark 基础旋转动画
- (CABasicAnimation *)rotationAnimation {
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAnimation.fromValue = [NSNumber numberWithFloat:-0.05];
    basicAnimation.toValue = [NSNumber numberWithFloat:0.05];
    basicAnimation.duration = 0.2;
    basicAnimation.repeatCount = CGFLOAT_MAX;
    basicAnimation.autoreverses = YES;
    return basicAnimation;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end