iOS UIView 动画浅谈

UIView 等会效果简单实现,哪一个登录页面的demo来举例子吧.

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;

UIView的一个类方法,可以看出这个方法里面只有两个参数:

duration: 动画持续时间;

animations:动画闭包,在这个闭包中你可以改变UIView的各种动画属性(这个方法中可以同时改变多个属性);

// 闭包里面是textField最终出现的位置,前提是你已经设置好了textField的位置.   [UIView animateWithDuration:0.5 animations:^{
       self.emailTextField.frame = CGRectMake(30, self.userImg.frame.origin.y + self.userImg.bounds.size.height + 30, screen_width - 60, 40);
       self.pwdTextField.frame = CGRectMake(30, self.emailTextField.frame.origin.y + self.emailTextField.bounds.size.height, screen_width - 60, 40);

   } completion:nil];

这个就是上面的代码动画出现的过程截图,emailText 和 pwdTextField 分别重两次移动到中间位置;

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;

这个方法多了几个参数:

delay:延时时间;

options:动画效果,比如重复动画、前后运动等;

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut  animations:^{
        self.emailTextField.frame = CGRectMake(30, self.userImg.frame.origin.y + self.userImg.bounds.size.height + 30, screen_width - 60, 40);
    } completion:nil];
    CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI);[UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationOptionCurveLinear animations:^{
        self.pwdTextField.frame = CGRectMake(30, self.emailTextField.frame.origin.y + self.emailTextField.bounds.size.height, screen_width - 60, 40);
        self.userImg.transform = rotation;
    } completion:nil];
    

这个过程里有头像的转动,emailText 和 pwdTextField 分别重两次移动到中间位置;

这两个添加动画效果的方法的代码附在下面:

//
//  ViewController.m
//  loginDemo20160330
//
//  Created by miaoguodong on 16/3/30.
//  Copyright © 2016年 miaoguodong. All rights reserved.
//

#import "ViewController.h"
#define screen_width ([UIScreen mainScreen].bounds.size.width)
#define screen_height ([UIScreen mainScreen].bounds.size.height)
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *loginBgImg;
@property (weak, nonatomic) IBOutlet UIImageView *userImg;

@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@end

@implementation ViewController
- (void)dealloc
{
    self.loginBgImg = nil;
    self.userImg = nil;
    self.emailTextField = nil;
    self.pwdTextField = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.loginBgImg.frame = CGRectMake(0, 0, screen_width, screen_height);
    self.loginBgImg.image = [UIImage imageNamed:@"BgImage"];

    self.userImg.frame = CGRectMake((self.loginBgImg.bounds.size.width - 100)/2, 80, 100, 100);
    self.userImg.image = [UIImage imageNamed:@"usrIcon"];

    self.emailTextField.frame = CGRectMake(screen_width, self.userImg.frame.origin.y + self.userImg.bounds.size.height + 30, screen_width - 60, 40);
    self.pwdTextField.frame = CGRectMake(-screen_width, self.emailTextField.frame.origin.y + self.emailTextField.bounds.size.height, screen_width - 60, 40);
//    self.emailTextField.frame = CGRectMake(30, self.userImg.frame.origin.y + self.userImg.bounds.size.height, screen_width - 60, 40);
//    self.pwdTextField.frame = CGRectMake(30, self.emailTextField.frame.origin.y + self.emailTextField.bounds.size.height, screen_width - 60, 40);

    UIView *emailLeftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.emailTextField.bounds.size.height, self.emailTextField.bounds.size.height)];
    UIImageView *eleftImg = [[UIImageView alloc]initWithFrame:CGRectMake(4, 4, 32, 32)];
    eleftImg.image = [UIImage imageNamed:@"user"];
    [emailLeftView addSubview:eleftImg];
    self.emailTextField.leftView = emailLeftView;
    self.emailTextField.leftViewMode = UITextFieldViewModeAlways;

    UIView *pwdLeftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.pwdTextField.bounds.size.height, self.pwdTextField.bounds.size.height)];
    UIImageView *pleftImg = [[UIImageView alloc]initWithFrame:CGRectMake(4, 4, 32, 32)];
    pleftImg.image = [UIImage imageNamed:@"pwd"];
    [pwdLeftView addSubview:pleftImg];
    self.pwdTextField.leftView = pwdLeftView;
    self.pwdTextField.leftViewMode = UITextFieldViewModeAlways;

    // 简单设置 text输入框出现

//   [UIView animateWithDuration:0.5 animations:^{
//       self.emailTextField.frame = CGRectMake(30, self.userImg.frame.origin.y + self.userImg.bounds.size.height + 30, screen_width - 60, 40);
//       self.pwdTextField.frame = CGRectMake(30, self.emailTextField.frame.origin.y + self.emailTextField.bounds.size.height, screen_width - 60, 40);
//
//   } completion:nil];

    // 设置 text输入框有动画效果的飞入
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut  animations:^{
        self.emailTextField.frame = CGRectMake(30, self.userImg.frame.origin.y + self.userImg.bounds.size.height + 30, screen_width - 60, 40);
    } completion:nil];
    CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI);
    [UIView animateWithDuration:0.5 delay:0.3 options:UIViewAnimationOptionCurveLinear animations:^{
        self.pwdTextField.frame = CGRectMake(30, self.emailTextField.frame.origin.y + self.emailTextField.bounds.size.height, screen_width - 60, 40);
        self.userImg.transform = rotation;
    } completion:nil];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewWillAppear:(BOOL)animated
{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
时间: 2024-08-03 11:18:09

iOS UIView 动画浅谈的相关文章

iOS UIView动画详解(Objective-C)

我在之前的一篇博客中<iOS UIView动画详解(Swift)>讲解了使用Swift来实现UIView类下面提供的多种动画效果,如位置动画.旋转动画.缩放动画.颜色动画.透明度动画等等.为了这个题目的完整性,今天我使用Objective-C来完全重写以上的所有的动画.项目案例已经上传至:https://github.com/chenyufeng1991/iOS-UIView-Animation  中的Animation-OC文件夹下,另一个目录下则是Swift实现的动画. (1)位置动画 P

iOS开发&gt;学无止境 - 浅谈MVVM的架构设计与团队协作

李刚按:本文是青玉伏案写的一篇文章.相信大家对MVC耳熟能详,MVVM可能听说的相对少一些,这一篇文章将会想你阐述MVVM设计,还有团队协作的经验分享.如果你也觉得不错,就分享一下吧! demo:https://github.com/lizelu/MVVM 今天写这篇文章是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇文章的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦~). 由 于本人项目经验有限,关于架构设

iOS开发之浅谈MVVM的架构设计与团队协作

今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦~). 由于本人项目经验有限,关于架构设计方面的东西理解有限,我个人对MVVM的理解主要是借鉴于之前的用过的MVC的Web框架~在学校的时候用过ThinkPHP框架,和SSH框架,都是MVC的架构模式,今天MVVM与传统的MVC可谓是极为相似,也可以说是兄弟关系,也就是一家人了. 说道架构设计和团队

iOS UIView动画详解

现在的iOS开发中,有很多的动画框架可以使用,包括苹果自带的CoreAnimation框架,Facebook的Pop等等,这些的确都是程序员的利器.但是如果我们仅仅是想要实现一些比较简单的动画呢?杀鸡焉用牛刀.我们直接用UIView就可以了.今天我们就来好好聊聊UIView动画,使用Swift编写(大家可以看到我有时候用OC,有时候用Swift,现在的iOS学习的基本技能看着OC代码能写出Swift,照着Swift能写出OC,哈哈).本示例代码上传至  https://github.com/ch

iOS应用架构浅谈

缘由 从事iOS工作一年多了,主要从事QQ钱包SDK开发和财付通app维护,随着对业务的慢慢熟悉,最近在思考这两款应用架构设计的思想,刚好昨天在微信里看了一篇iOS大牛对终端应用架构的分享,乘热打铁,下面浅谈下我对ios应用架构设计的理解,写的不好或不对的地方,欢迎大家拍砖,我们一起来探讨. 假如问你一个iOS or Android app的架构,你会从哪些方面来说呢? 不要急着给出你的答案,可以先在你的脑子里思考3分钟,再看下面我要讲的内容. 其实对于iOS客户端应用的架构来说,复杂度不亚于服

iOS UIView动画实践(一):揭开Animation的神秘面纱

前言 在一个看脸的社会中,不论什么事物,长得好看总是能多吸引一些目光.App同样不例外,一款面相不错的App就算功能已经被轮子千百遍,依然会有人买账,理由就是看得顺眼,于是平面设计人员越来越被重视.白驹过隙,斗转星移,人们已然不满足于静态的美感,于是动态的用户体验应运而生,平面设计人员捉襟见肘,是我们程序员出马的时候了. 这篇文章是UIView Animation的第一篇,从极简的概念开始,为大家揭开Animation的神秘面纱.我们以一个登录界面为例.美丽的太阳,婀娜的云,还有几个小山包,中间

前端知识 | React Native Animated动画浅谈

在移动开发中,动画能有效的提高用户体验.在 React Native 中,也有相应的 API 供我们做动画.这里着重说一下 Animated 动画库,它可以让我们非常简便的去实现各式各样的动画和交互方式,而且具备很高的性能.Animated 目前只封装了四个可以动画化的组件:View.Text.Image.ScrollView,不过你也可以用 Animated.createAnimatedComponent() 来封装你自己的组件. 话不多说,我们来举个栗子: 步骤拆解 1.创建 Animate

iOS之block浅谈

前言 ios4.0系统已开始支持block,在编程过程中,block被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.它和传统的函数指针很类似,但是有区别:block是inline的,并且它对局部变量是只读的. block和函数的相似性:(1)可以保存代码(2)有返回值(3)有形参(4)调用方式一样. block的使用 1.block的定义 // 声明和实现写在一起,就像变量的声明实现 int a

iOS UIView动画效果 学习笔记

CGRect frame = _confirmV.frame; [UIView beginAnimations:nil context:nil];//动画定义开始 [UIView setAnimationDuration:0.5];//动画的时长 [UIView setAnimationDelay:0]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(removeConfirmV