iOS带弹跳动画发布界面

2023-10-31 20:50
文章标签 界面 发布 ios 动画 弹跳

本文主要是介绍iOS带弹跳动画发布界面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

项目中经常会用到带弹跳动画发布界面


//  PublishView.m

//  UIImage+ImageEffects.h 苹果蒙化图片的分类 pop.h弹跳动画框架 EJExtension.h模型转换框架

// ComposeModel 用于设置按钮文字与图片的模型,在本地设置plist文件保存image(按钮图片)和text(按钮文字)

#import "PublishView.h"

#import "BSVerticalButton.h"

#import "UIImage+ImageEffects.h"

#import "pop.h"

#import "MJExtension.h"

#import "ComposeModel.h"

@interface PublishView ()

/** 取消按钮 */

@property (nonatomic, weak) UIButton *cancelButton;

@end

@implementation PublishView

/** 全局 window_ */

static UIWindow *window_;

/** 显示发布view */

+ (void)show{

    // 添加一个独立的window是为了隔离点击事件

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

    

    window_.hidden = NO;

    

    PublishView *publish = [[PublishView alloc]init];

    

    publish.frame = window_.bounds;

    

    [window_ addSubview:publish];

}

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        

        UIImageView *imageView = [[UIImageView alloc]initWithImage:[self getEffectImage]];

        [self addSubview:imageView];

        [self setupUI];

    }

    return self;

}

- (void)setupUI{

    

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

  

    // 按钮弹跳动画时让view本身不能点击

    self.userInteractionEnabled = NO;

    

    // plis文件获得一个模型数组

    NSArray *buttonModelArray = [ComposeModel mj_objectArrayWithFilename:@"buttonImage.plist"];

    

    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 < buttonModelArray.count; ++i) {

        

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

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

        

        button.tag = i;

        

        [self addSubview:button];

        

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

        

        ComposeModel *composeModel = buttonModelArray[i];

        

        [button setImage:[UIImage imageNamed:composeModel.image] forState:UIControlStateNormal];

        

        [button setTitle:composeModel.text 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];

    }

    

    // 添加品牌logo

    UIImageView *topImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"compose_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() + buttonModelArray.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];

    // 底部取消按钮

    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];

    [cancelButton setTitle:@" " forState:UIControlStateNormal];

    cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];

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

    [cancelButton setBackgroundColor:[UIColor whiteColor]];

    [cancelButton addTarget:self action:@selector(cancelButtonClick:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:cancelButton];

    self.cancelButton = cancelButton;

}

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

    

    // 退出时执行动画  方法的参数block传空

    [self animationWithBlock:nil];

}

- (void)layoutSubviews{

    [super layoutSubviews];

    // 取消按钮位置大小

    CGPoint center = self.cancelButton.center;

    center.x = self.center.x;

    self.cancelButton.center = center;

    CGRect frame = self.cancelButton.frame;

    frame.origin.y = self.frame.size.height * 0.85;

    frame.size = CGSizeMake(200, 35);

    self.cancelButton.frame = frame;

}

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

    

    [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;

        }

    }];

    

}

/** 退出时与点出了某个按钮时执行的弹跳动画后销毁 window_ 移除 这个蒙板 view ,如果block参数completionBlock有值先销毁window_后再执行这个block里的代码块 */

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

    

    NSLog(@"----%@\n",self.subviews);

    

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

    // view本身不能点

    self.userInteractionEnabled = NO;

    // 选移除取消按钮

    [self.cancelButton removeFromSuperview];

    

    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];

        

        CGPoint center = view.center; //取出中心点

        

        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(center.x ,  center.y + [[UIScreen mainScreen] bounds].size.height)];

        

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

            // 动画结束时调用的 block

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

            

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

                

                [self removeFromSuperview];

                

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

       

                !completionBlock ? : completionBlock();

            };

        }

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

        [view pop_addAnimation:animation forKey:nil];

    }

}

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

    

    [self animationWithBlock:nil];

}

// 获得一个磨纱蒙板 image 图片

- (UIImage *)getEffectImage{

    UIWindow *window = [UIApplication sharedApplication].keyWindow; //获取当前 window

    UIGraphicsBeginImageContext(window.size); //开启window大小的图形上下文

    CGContextRef ref = UIGraphicsGetCurrentContext(); //开启图形上下文

    [window.layer renderInContext:ref]; //window图层 渲染到图形上下文当中

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); //获取图片

    UIGraphicsEndImageContext(); //关闭图形上下文

    image = [image applyLightEffect]; //调用 image 分类方法 使图片调成蒙板状态

    return image;

}

@end


项目中用到的垂直布局自定义按钮 BSVerticalButton

#import "BSVerticalButton.h"

@implementation BSVerticalButton

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        [self setupUI];

    }

    return self;

}

- (void)awakeFromNib{

    [super awakeFromNib];

    

    [self setupUI];

}

- (void)setupUI{

    self.titleLabel.textAlignment = NSTextAlignmentCenter;

}

- (void)layoutSubviews{

    [super layoutSubviews];

    

    //按钮内部图片 frame

    CGRect imageViewFrame = self.imageView.frame;

    imageViewFrame.origin.x = 0;

    imageViewFrame.origin.y = 0;

    imageViewFrame.size.width = self.bounds.size.width;

    imageViewFrame.size.height = self.bounds.size.width;

    self.imageView.frame = imageViewFrame;

    

    //按钮内部label frame

    CGRect titleLabelFrame = self.titleLabel.frame;

    titleLabelFrame.origin.x = 0;

    titleLabelFrame.origin.y = self.imageView.frame.size.height + 10;

    titleLabelFrame.size.width = self.bounds.size.width;

    self.titleLabel.frame = titleLabelFrame;

    

    //按钮自身大小

    CGRect buttonBounds = self.bounds;

    buttonBounds.size.width = self.imageView.frame.size.width;

    buttonBounds.size.height = self.imageView.bounds.size.height + self.titleLabel.bounds.size.height + 10;

    self.bounds = buttonBounds;

}

@end


这篇关于iOS带弹跳动画发布界面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/318056

相关文章

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

macOS Sequoia 15.5 发布: 改进邮件和屏幕使用时间功能

《macOSSequoia15.5发布:改进邮件和屏幕使用时间功能》经过常规Beta测试后,新的macOSSequoia15.5现已公开发布,但重要的新功能将被保留到WWDC和... MACOS Sequoia 15.5 正式发布!本次更新为 Mac 用户带来了一系列功能强化、错误修复和安全性提升,进一步增

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Maven 依赖发布与仓库治理的过程解析

《Maven依赖发布与仓库治理的过程解析》:本文主要介绍Maven依赖发布与仓库治理的过程解析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录Maven 依赖发布与仓库治理引言第一章:distributionManagement配置的工程化实践1

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

售价599元起! 华为路由器X1/Pro发布 配置与区别一览

《售价599元起!华为路由器X1/Pro发布配置与区别一览》华为路由器X1/Pro发布,有朋友留言问华为路由X1和X1Pro怎么选择,关于这个问题,本期图文将对这二款路由器做了期参数对比,大家看... 华为路由 X1 系列已经正式发布并开启预售,将在 4 月 25 日 10:08 正式开售,两款产品分别为华

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析