POP彈性動畫效果

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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章