码迷,mamicode.com
首页 > 其他好文 > 详细

OC里在UIView上实现带弹跳动画按钮

时间:2017-07-14 18:05:15      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:int   led   nsa   dia   touch   center   instance   行号   段子   

 

/*这时用到 pop框架 自定义按钮 BSVerticalButton*/

// 加载了一个 用xib描述的这个UIView

+ (instancetype)publishView{

    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;

// 定义一个全局 window_

static UIWindow *window_;

+ (void)show{

    //这里用自定义的 window 是为了隔绝点击事件 不让点击事件传到后面控制器的view上去

    window_ = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    // 这时也可以设置 window的半透明 不用背景图片

//    window_.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];

    // 这里publishView 是加载了一个 用xib描述的这个UIView

    BSPublishView *publishView = [BSPublishView publishView];

    publishView.frame = window_.bounds;

    window_.hidden = NO; //显示开启window

    [window_ addSubview:publishView];

}

 

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        [self setupUI];

    }

    return self;

}

 

- (void)awakeFromNib{

    [super awakeFromNib];

    [self setupUI];

}

- (void)setupUI{

    //这里用自定义的 window 是为了隔绝点击事件 不让点击事件传到后面控制器的view上去

    // view本身不能点

    self.userInteractionEnabled = NO;

    NSArray *buttonImages = @[@"publish-video",@"publish-picture",@"publish-text",@"publish-audio",@"publish-review",@"publish-offline"];

    NSArray *buttonTitles = @[@"发视频",@"发图片",@"发段子",@"发声音",@"审贴子",@"离线下载"];

    CGFloat button_w = 72;

    CGFloat button_h = button_w + 30;

    NSInteger maxLoc = 3; //最多列数

    //按钮弹跳动画停止后的起始 y 值

    CGFloat buttonEnd_y = ([[UIScreen mainScreen] bounds].size.height - button_h * 2) / 2;

    //最开始在屏幕外上方的的起始 y 值

    CGFloat buttonBegin_y = buttonEnd_y - [[UIScreen mainScreen] bounds].size.height;

    //按钮的起始间隙值

    CGFloat buttonStartMargin = 20;

    //中间的一个按钮相对于两边按钮的间隙

    CGFloat buttonMargin = ([[UIScreen mainScreen] bounds].size.width - buttonStartMargin * 2 - button_w * maxLoc) / (maxLoc - 1);

    for (NSInteger i = 0; i < buttonImages.count; ++i) {

        // BSVerticalButton 自定义的垂直排布按钮

        BSVerticalButton *button = [[BSVerticalButton alloc]init];

        button.tag = i;

        [self addSubview:button];

        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        [button setImage:[UIImage imageNamed:buttonImages[i]] forState:UIControlStateNormal];

        [button setTitle:buttonTitles[i] forState:UIControlStateNormal];

        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        button.titleLabel.font = [UIFont systemFontOfSize:14];

        NSInteger loc = i % maxLoc;   //例号

        NSInteger row = i / maxLoc;   //行号

        CGFloat button_x = buttonStartMargin + loc * (button_w + buttonMargin);

        CGFloat buttonBginAnimation_y = buttonBegin_y + (button_h * row); //弹跳前的 y 值

        CGFloat buttonEndAnimation_y = buttonEnd_y + (button_h * row); //弹跳后的 y 值  

        //创建pop弹簧动画对象

        POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];

        animation.beginTime = CACurrentMediaTime() + i * 0.1; //动画开始时间 

        animation.springBounciness = 10; //弹簧增强 0-20

        animation.springSpeed = 8; //弹簧速度 0-20

        animation.fromValue = [NSValue valueWithCGRect:CGRectMake(button_x, buttonBginAnimation_y, button_w, button_h)];

        animation.toValue = [NSValue valueWithCGRect:CGRectMake(button_x, buttonEndAnimation_y, button_w, button_h)];

        //中间的按钮添加动画

        [button pop_addAnimation:animation forKey:nil];

    }

    UIImageView *topImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];

    topImageView.center = CGPointMake([[UIScreen mainScreen] bounds].size.width * 0.5, [[UIScreen mainScreen] bounds].size.height * 0.2 - [[UIScreen mainScreen] bounds].size.height);

    [self addSubview:topImageView];

//    POPBasicAnimation    基本的动画

//    POPSpringAnimation   弹簧动画

//    POPDecayAnimation    减速动画

//    POPCustomAnimation   自定义动画

    //创建pop弹簧动画对象

    POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];

    animation.beginTime = CACurrentMediaTime() + buttonImages.count * 0.001; //动画开始时间

    animation.springBounciness = 10; //弹簧增强 0-20

    animation.springSpeed = 10; //弹簧速度 0-20

    CGFloat center_x = [[UIScreen mainScreen] bounds].size.width * 0.5;

    CGFloat endCenter_y = [[UIScreen mainScreen] bounds].size.height * 0.2;

    CGFloat beginCenter_y = endCenter_y - [[UIScreen mainScreen] bounds].size.height;

    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(center_x, beginCenter_y)];

    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(center_x, endCenter_y)];

    animation.completionBlock = ^(POPAnimation *anim, BOOL finished){

        NSLog(@"-------这里可以写动画结束后所要执行的代码...");

        // view本身开启交互

        self.userInteractionEnabled = YES;

    };

    //给顶部的图片添加动画

    [topImageView pop_addAnimation:animation forKey:nil];

}

- (void)buttonClick:(UIButton *)button{

    [self cancel];

    [self animationWithBlock:^{

        switch (button.tag) {

            case 0:

                NSLog(@"发视频");

                break;

            case 1:

                NSLog(@"发图片");

                break;

            case 2:

                NSLog(@"发段子"); 

                break;

            case 3:

                NSLog(@"发声音");

                break;

            case 4:

                NSLog(@"审贴子");

                break;

            case 5:

                NSLog(@"离线下载");

                break;

                

            default:

                break;

        }

    }];

}

- (void)animationWithBlock:(void (^) ())completionBlock{

    //这里用自定义的 window 是为了隔绝点击事件 不让点击事件传到后面控制器的view上去

    // view本身不能点

    self.userInteractionEnabled = NO;

    for (NSInteger i = 1; i < self.subviews.count; ++i) {

        UIView *view = self.subviews[i];

        //创建pop基本动画对象

        POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];

        //        POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];

        animation.beginTime = CACurrentMediaTime() + (i-1) * 0.1; //动画开始时间        

        // 如果用这个基类 POPBasicAnimation  动画的执行节奏(一开始很慢, 后面很快)

        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(view.z_centerX,  view.z_centerY + [[UIScreen mainScreen] bounds].size.height)];

        if (i == self.subviews.count - 1) { //说明是最后一个 view在做动画,就让执行结束的 block

            // 动画结束时调用的 block

            animation.completionBlock = ^(POPAnimation *anim, BOOL finished){

                window_ = nil; //销毁自定义的 window

           //这里等窗口和动画完成后再执行这个block

                !completionBlock ? : completionBlock();

            };

        }

        //给顶部的图片添加动画

        [view pop_addAnimation:animation forKey:nil];

    }

}

 

// 退出

- (IBAction)cancel {

    [self animationWithBlock:nil];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self animationWithBlock:nil];

}

 

OC里在UIView上实现带弹跳动画按钮

标签:int   led   nsa   dia   touch   center   instance   行号   段子   

原文地址:http://www.cnblogs.com/chenweb/p/7171656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!