POP弹性动画效果

该博客介绍了如何使用POP库创建弹性动画,使彩色按钮从顶部依次下落并带有弹簧效果。自定义的窗口和按钮在用户交互后通过block回调执行点击事件。详细展示了代码实现过程,包括添加子控件、设置动画和点击事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.中间的彩色按钮是用POP做的弹性动画依次下落到指定位置,并有弹簧效果

2.彩色Button为自定义button 

3.透明背景为自定义window,在modal情况下,覆盖的控制器会被移除,在dismiss时重新添加,因此改为添加一个新的窗口;

4.在移除动画时,用block回调,执行按钮点击后的操作


#import "ZHPublicView.h"

#import <POP.h>

#import "ZHbutton.h"



@implementation ZHPublicView


//点击底部加号按钮,对外接口方法

+(void)show{


     UIView *publicView = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil]firstObject];

    publicView.frame = window_.bounds;

    [window_ addSubview:publicView];

}

static UIWindow *window_;

//一次性操作

-(void)awakeFromNib{

    

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

    window_.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.8];

    window_.hidden = NO;

    self.userInteractionEnabled = NO;

    [self setUptext];

    [self addAllChildButton];



}

//添加子控件,图片为本地bundle图片

-(void)addAllChildButton{

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

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

    

    //按钮的frame

    NSInteger col = 3;

    CGFloat margin = 10;

    CGFloat buttonW = 80;

    CGFloat buttonH = 100;

    

    CGFloat marginX = ([UIScreen mainScreen].bounds.size.width - 2 * margin - col * buttonW) / 2;



    CGFloat beginY = [UIScreen mainScreen].bounds.size.height * 0.5 - buttonH;

    for (NSInteger i = 0; i < 6; i++) {


        CGFloat x = margin + (buttonW + marginX)*(i % col);

        CGFloat y = beginY + (buttonH + margin)*(i / col);


        ZHbutton *button = [ZHbutton buttonWithType:UIButtonTypeCustom];

        button.tag = i;

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

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

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

        

        CGRect beginFrame = CGRectMake(x, y - 375, buttonW, buttonH);;

        CGRect endFrame = CGRectMake(x, y, buttonW, buttonH);

        button.frame = endFrame;

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

        [self addSubview:button];

        

        //POP动画

        POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];

        spring.fromValue = [NSValue valueWithCGRect:beginFrame];

        spring.toValue = [NSValue valueWithCGRect:endFrame];

        spring.springBounciness = 12;

        spring.springSpeed = 8;

        spring.beginTime = CACurrentMediaTime() + 0.05 * i;

        [button pop_addAnimation:spring forKey:nil];

  }

}


-(void)cancelButtonWithCompleBlock:(void (^)())completion{

    

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

        UIView *subView = self.subviews[i];

        POPBasicAnimation *spring = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];

        CGFloat centerY = [UIScreen mainScreen].bounds.size.height + subView.ZH_Y;

        spring.toValue = [NSValue valueWithCGPoint:CGPointMake(subView.ZH_centerX, centerY)];

        

        spring.beginTime = CACurrentMediaTime() + i * 0.1;

        

        [subView pop_addAnimation:spring forKey:nil];

        

        if (i == self.subviews.count - 1) {

            [spring setCompletionBlock:^(POPAnimation *anima, BOOL comple) {

                

                window_.hidden = YES;

                window_ = nil;

                !completion ? :completion();

            }];

        }

    }

}


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

  

    [self cancelButtonWithCompleBlock:^{

        NSLog(@"%ld",button.tag); //点击按钮操作事件

    }];

  

}


-(void)setUptext{

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

    [self addSubview:imageView];

    

    POPSpringAnimation *spring = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];

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

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

    spring.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX , centerY - 200)];

    spring.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerY)];

    spring.springBounciness = 20;

    spring.springSpeed = 18;

    spring.beginTime = CACurrentMediaTime() + 6 * 0.05;

    [spring setCompletionBlock:^(POPAnimation * anima, BOOL complish) {

        self.userInteractionEnabled = YES;

    }];

    [imageView pop_addAnimation:spring forKey:nil];

    

}


- (IBAction)cancelButton{

    

    [self cancelButtonWithCompleBlock:nil];


}

@end


#import <UIKit/UIKit.h>


@interface ZHPublicView : UIView


+(void)show;


@end


/自定义button


#import "ZHbutton.h"


@implementation ZHbutton


-(void)layoutSubviews{


    [super layoutSubviews];

    

    self.imageView.ZH_Y = 0;

    self.imageView.ZH_centerX = self.ZH_Width * 0.5;

    


    self.titleLabel.ZH_Y = self.imageView.ZH_Height + 3;

    [self.titleLabel sizeToFit];

    self.titleLabel.ZH_centerX = self.ZH_Width * 0.5;

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值