UIButton案例之添加动画

2024-05-14 01:36
文章标签 案例 动画 uibutton

本文主要是介绍UIButton案例之添加动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求

基于上一节代码进行精简,降低了冗余性。添加动画,使得坐标变化自然,同时使用了bounds属性和center属性,使得UIView变化以中心点为基准。
此外,使用两种方式添加动画:1.原始方式。 2.block方式。

代码实现

@interface UIButtonTest1 : UIView@property(strong, nonatomic) UIButton *btn;
@property(strong, nonatomic) UIButton *btn1;
@property(strong, nonatomic) UIButton *btn2;
@property(strong, nonatomic) UIButton *btn3;
@property(strong, nonatomic) UIButton *btn4;
@property(strong, nonatomic) UIButton *btn5;
@property(strong, nonatomic) UIButton *btn6;@end
#import "UIButtonTest1.h"@implementation UIButtonTest1-(instancetype) initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];// 视图中有个按钮,按钮点击了没反应?GPT4实现一下if(self){_btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];[_btn setTitle:@"点击前" forState:UIControlStateNormal];// 文字设置[_btn setTitleColor:[UIColor blackColor]  forState:UIControlStateNormal];// bgImage[_btn setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];// 动画:绑定函数,点击事件,在btn上调用,加到self上[_btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];/// btn1~6 初始化和设置大小_btn1 = [[UIButton alloc] initWithFrame:CGRectMake(80, 350, 50, 50)];_btn2 = [[UIButton alloc] initWithFrame:CGRectMake(80, 400, 50, 50)];_btn3 = [[UIButton alloc] initWithFrame:CGRectMake(30, 400, 50, 50)];_btn4 = [[UIButton alloc] initWithFrame:CGRectMake(130, 400, 50, 50)];_btn5 = [[UIButton alloc] initWithFrame:CGRectMake(200, 349, 49, 49)];_btn6 = [[UIButton alloc] initWithFrame:CGRectMake(200, 402, 49, 49)];/// 设置背景图片[_btn1 setBackgroundImage:[UIImage imageNamed:@"shang.jpg"] forState:UIControlStateNormal];[_btn2 setBackgroundImage:[UIImage imageNamed:@"xia.jpg"] forState:UIControlStateNormal];[_btn3 setBackgroundImage:[UIImage imageNamed:@"zuo.jpg"] forState:UIControlStateNormal];[_btn4 setBackgroundImage:[UIImage imageNamed:@"you.jpg"] forState:UIControlStateNormal];[_btn5 setBackgroundImage:[UIImage imageNamed:@"jia.jpg"] forState:UIControlStateNormal];[_btn6 setBackgroundImage:[UIImage imageNamed:@"jian.jpg"] forState:UIControlStateNormal];// 设置不同tag以区分不同按钮_btn1.tag = 1;_btn2.tag = 2;_btn3.tag = 3;_btn4.tag = 4;_btn5.tag = 5;_btn6.tag = 6;/// 绑定函数:按钮的反应函数都绑定到同一个函数上/// 有参数这里要加:吗
//        [_btn1 addTarget:self action:@selector(btn1Clicked:) forControlEvents:UIControlEventTouchUpInside];
//        [_btn2 addTarget:self action:@selector(btn1Clicked:) forControlEvents:UIControlEventTouchUpInside];
//        [_btn3 addTarget:self action:@selector(btn1Clicked:) forControlEvents:UIControlEventTouchUpInside];
//        [_btn4 addTarget:self action:@selector(btn1Clicked:) forControlEvents:UIControlEventTouchUpInside];
//        [_btn5 addTarget:self action:@selector(btn1Clicked:) forControlEvents:UIControlEventTouchUpInside];
//        [_btn6 addTarget:self action:@selector(btn1Clicked:) forControlEvents:UIControlEventTouchUpInside];
//        // 绑定第二种[_btn1 addTarget:self action:@selector(btn2Clicked:) forControlEvents:UIControlEventTouchUpInside];[_btn2 addTarget:self action:@selector(btn2Clicked:) forControlEvents:UIControlEventTouchUpInside];[_btn3 addTarget:self action:@selector(btn2Clicked:) forControlEvents:UIControlEventTouchUpInside];[_btn4 addTarget:self action:@selector(btn2Clicked:) forControlEvents:UIControlEventTouchUpInside];[_btn5 addTarget:self action:@selector(btn2Clicked:) forControlEvents:UIControlEventTouchUpInside];[_btn6 addTarget:self action:@selector(btn2Clicked:) forControlEvents:UIControlEventTouchUpInside];// 初始化btn2、btn3、btn4、btn5// 本身是view,需要添加组件进去[self addSubview:_btn];[self addSubview:_btn1];[self addSubview:_btn2];[self addSubview:_btn3];[self addSubview:_btn4];[self addSubview:_btn5];[self addSubview:_btn6];}return self;
}// 带图片的按钮点击后的变化
// 点击后重新设置title、bgImage
- (void)btnClicked{// 点击前后static BOOL isClicked = NO;if(isClicked){[_btn setTitle:@"点击前" forState:UIControlStateNormal];[_btn setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];}else{// 状态常识不同样式:hightlight,可设置[_btn setTitle:@"点击后" forState:UIControlStateNormal];[_btn setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];}isClicked = !isClicked;
}// 增加动画:以原始坐标变化
- (void)btn1Clicked:(UIButton *)sender{// 获取原始frameCGRect originalFrame = self.btn.frame;switch (sender.tag) {case 1:originalFrame.origin.y -= 100;break;case 2:originalFrame.origin.y += 100;break;case 3:originalFrame.origin.x -= 100;break;case 4:originalFrame.origin.x += 100;break;case 5:originalFrame.size.width += 100;originalFrame.size.height += 100;break;case 6:originalFrame.size.width -= 100;originalFrame.size.height -= 100;break;}// ==== 改:动画 ====// 开启[UIView beginAnimations:nil context:nil];// 设置动画时间[UIView setAnimationDuration:2];// 要执行的动画self.btn.frame = originalFrame;[UIView commitAnimations];// 发现就是在frame变化的前后增加动画开启、设置动画时长和关闭设置
}// 增加动画:以中心点变化
// center:更改放大缩小,移动看不出的
- (void)btn2Clicked:(UIButton *)sender{// 获取原始frame:中心变化center用这个CGPoint centerPoint = self.btn.center;CGRect originBounds = self.btn.bounds;switch (sender.tag) {case 1:centerPoint.y -= 100;break;case 2:centerPoint.y += 100;break;case 3:centerPoint.x -= 100;break;case 4:centerPoint.x += 100;break;case 5:originBounds.size.width += 100;originBounds.size.height += 100;break;case 6:originBounds.size.width -= 100;originBounds.size.height -= 100;break;}// ==== 改:动画 ====// 开启[UIView beginAnimations:nil context:nil];// 设置动画时间[UIView setAnimationDuration:2];// 要执行的动画self.btn.center = centerPoint;self.btn.bounds = originBounds;[UIView commitAnimations];// 发现就是在frame变化的前后增加动画开启、设置动画时长和关闭设置
}@end

其实点击后的按钮变化可以通过设置普通状态和高亮状态来做切换,以上代码是按监听后状态变化来实现的

  1. 将各事件响应函数封装到了一起,需要利用tag属性。
  2. @selector()绑定函数时,如果有参数,需要加冒号,如果没有参数,直接加名字即可。
  3. center和bounds的初始化类和frame不同,要注意

以上为头尾式实现添加动画,下面使用block方式添加动画。

使用block方式实现动画

// 使用block方式
[UIView animateWithDuration:1.0 animations:^{self.btn.frame = originalFrame;
}]

复习OC的block
这是一种函数调用的简写方式

这篇关于UIButton案例之添加动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red

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

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

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

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

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

MySQL 复合查询案例详解

《MySQL复合查询案例详解》:本文主要介绍MySQL复合查询案例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录基本查询回顾多表笛卡尔积子查询与where子查询多行子查询多列子查询子查询与from总结合并查询(不太重要)union基本查询回顾查询

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地