POP弹性动画效果

来源:互联网 发布:淘宝客开发论坛 编辑:程序博客网 时间:2024/05/19 03:28

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 = [[[NSBundlemainBundle]loadNibNamed:NSStringFromClass(self)owner:niloptions:nil]firstObject];

    publicView.frame = window_.bounds;

    [window_ addSubview:publicView];

}

static UIWindow *window_;

//一次性操作

-(void)awakeFromNib{

    

    window_ = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

    window_.backgroundColor = [[UIColorwhiteColor]colorWithAlphaComponent:0.8];

    window_.hidden =NO;

    self.userInteractionEnabled =NO;

    [selfsetUptext];

    [selfaddAllChildButton];



}

//添加子控件,图片为本地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 = ([UIScreenmainScreen].bounds.size.width -2 * margin - col * buttonW) / 2;



    CGFloat beginY = [UIScreenmainScreen].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 = [ZHbuttonbuttonWithType:UIButtonTypeCustom];

        button.tag = i;

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

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

        [button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

        

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

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

        button.frame = endFrame;

        [button setImage:[UIImageimageNamed:image[i]] forState:UIControlStateNormal];

        [self addSubview:button];

        

        //POP动画

        POPSpringAnimation *spring = [POPSpringAnimationanimationWithPropertyNamed:kPOPViewFrame];

        spring.fromValue = [NSValuevalueWithCGRect:beginFrame];

        spring.toValue = [NSValuevalueWithCGRect: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 = [POPBasicAnimationanimationWithPropertyNamed:kPOPViewCenter];

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

        spring.toValue = [NSValuevalueWithCGPoint: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{

  

    [selfcancelButtonWithCompleBlock:^{

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

    }];

  

}


-(void)setUptext{

    UIImageView *imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"app_slogan"]];

    [self addSubview:imageView];

    

    POPSpringAnimation *spring = [POPSpringAnimationanimationWithPropertyNamed:kPOPViewCenter];

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

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

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

    spring.toValue = [NSValuevalueWithCGPoint: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{

    

    [selfcancelButtonWithCompleBlock:nil];


}

@end


#import <UIKit/UIKit.h>


@interface ZHPublicView : UIView


+(void)show;


@end


/////////////////////////////自定义button


#import "ZHbutton.h"


@implementation ZHbutton


-(void)layoutSubviews{


    [superlayoutSubviews];

    

    self.imageView.ZH_Y =0;

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

    


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

    [self.titleLabelsizeToFit];

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

}


@end



0 0
原创粉丝点击