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

按钮来回晃动的动画.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值