UI07UIView动画

本文通过一个具体的UIKit动画案例,展示了如何利用iOS中的UIView动画方法来实现包括位移、缩放、旋转、变色和透明度等在内的多种动画效果,并通过手势触发动画序列。

ViewController.m

#import "ViewController.h"
#import "ImageAnimationViewController.h"
@interface ViewController ()
//黑色视图
@property (nonatomic, strong) UIView *blackView;
//导航栏的按钮点击事件
- (IBAction)nextVC:(id)sender;
//初始化界面
- (void)initUserInterface;
#pragma mark - UIView动画
////手势方法
//- (void)tapProcess;
////位移
//- (void)positionAnimation;
////缩放
//- (void)scaleAnimation;
////旋转
//- (void)rotationAnimation;
////变色
//- (void)colorAnimation;
////透明度
//- (void)alphAnimation;
////结束
//- (void)endAnimation;
////UIView动画回调方法
//- (void)showViewAnimationDidStop:(NSString *)animationID;
#pragma mark - block方法
//位移
- (void)positionAnimationBlock;
//缩放
- (void)scaleAnimationBlock;
//旋转
- (void)rotationAnimationBlock;
//颜色
- (void)colorAnimationBlock;
//透明度
- (void)alphAnimationBlock;
//返回
- (void)endAnimationBlock;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    
}

#pragma mark - initUserInterface method
- (void)initUserInterface
{
    //黑色背景视图
    self.view.backgroundColor = [UIColor whiteColor];
    _blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    _blackView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_blackView];
    
    /**< 为黑色背景视图添加点击手势 */
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapProcess)];
    [_blackView addGestureRecognizer:tapGesture];
}

#pragma mark - 手势方法
- (void)tapProcess{
    [self positionAnimationBlock];
    
}

#pragma block动画
//位移
- (void)positionAnimationBlock{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)- 100);
    } completion:^(BOOL finished) {
        [self scaleAnimationBlock];
    }];
}

//缩放
- (void)scaleAnimationBlock{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        [self rotationAnimationBlock];
    }];
}

//旋转
- (void)rotationAnimationBlock{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.transform = CGAffineTransformRotate(_blackView.transform, M_PI);
    } completion:^(BOOL finished) {
        [self colorAnimationBlock];
    }];
}

//变色
- (void)colorAnimationBlock
{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.backgroundColor = [UIColor redColor];
    } completion:^(BOOL finished) {
        [self alphAnimationBlock];
    }];
}

//透明度(渐变)
- (void)alphAnimationBlock
{
    [UIView animateWithDuration:0.5 animations:^{
        _blackView.alpha = 0.5;
    } completion:^(BOOL finished) {
        [self endAnimationBlock];
    }];
}

//结束
- (void)endAnimationBlock{
    [UIView animateWithDuration:1 animations:^{
        //视图恢复到初始状态
        _blackView.backgroundColor = [UIColor blackColor];
        _blackView.alpha = 1.0;
        _blackView.center = CGPointMake(CGRectGetMidX(_blackView.bounds), CGRectGetMidX(_blackView.bounds));
        
        // CGAffineTransformIdentity:移除所有的变幻属性
        _blackView.transform = CGAffineTransformIdentity;
        
        //转场动画(效果)
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_blackView cache:NO];
    } completion:^(BOOL finished) {

    }];
}

#pragma mark - UIView动画
////位移
//- (void)positionAnimation{
//    //开始动画
//    [UIView beginAnimations:@"position" context:nil];
//    //持续时间
//    [UIView setAnimationDuration:1];
//    //线性规律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //设置回调方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    //核心动画
//    _blackView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame)- 100);
//    //提交动画
//    [UIView commitAnimations];
//}
//
////缩放
//- (void)scaleAnimation{
//    //开始动画
//    [UIView beginAnimations:@"scale" context:nil];
//    //持续时间
//    [UIView setAnimationDuration:0.7];
//    //线性规律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //设置回调方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    
//    //核心动画
//    /**<  设置动画类型:缩放
//      transform:变化属性(可实现旋转和所缩放动画)
//      CGAffineTransformScale...:在新生成的属性变换基础之上继续执行
//      CGAffineTransformMakeScale...:在最初始(最原始)的属性基础上做变化 */
//    
//    _blackView.transform = CGAffineTransformMakeScale(1.5, 1.5);
//    
//    //在变化后的基础上还可持续放大
//    //_blackView.transform = CGAffineTransformScale(_blackView.transform, 1.5, 1.5);
//    //提交动画
//    [UIView commitAnimations];
//}
//
////旋转
//- (void)rotationAnimation{
//    //开始动画
//    [UIView beginAnimations:@"rotation" context:nil];
//    //持续时间
//    [UIView setAnimationDuration:0.5];
//    //线性规律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //设置回调方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    
//    // 设置动画类型:旋转
//    // angle:角度
//    // M_PI:180°
//    // M_PI_2:90°
//    // M_PI_4:45°
//    //旋转之后缩回原来的大小
//    //_blackView.transform = CGAffineTransformMakeRotation(M_PI);
//    //旋转之后保持放大后的大小
//    _blackView.transform = CGAffineTransformRotate(_blackView.transform, M_PI);
//    //提交动画
//    [UIView commitAnimations];
//    
//}
//
////变色
//- (void)colorAnimation{
//    //开始动画
//    [UIView beginAnimations:@"color" context:nil];
//    //持续时间
//    [UIView setAnimationDuration:0.5];
//    //线性规律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //设置回调方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    //核心动画
//    _blackView.backgroundColor = [UIColor redColor];
//     //提交动画
//     [UIView commitAnimations];
//}
//
////透明度
//- (void)alphAnimation{
//    //开始动画
//    [UIView beginAnimations:@"alph" context:nil];
//    //持续时间
//    [UIView setAnimationDuration:0.5];
//    //线性规律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //设置回调方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    //核心动画
//     _blackView.alpha = 0.5;
//    //提交动画
//    [UIView commitAnimations];
//    
//}
//
////结束
//- (void)endAnimation{
//    //开始动画
//    [UIView beginAnimations:@"end" context:nil];
//    //持续时间
//    [UIView setAnimationDuration:1];
//    //线性规律
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//    //添加代理
//    [UIView setAnimationDelegate:self];
//    //设置回调方法
//    [UIView setAnimationDidStopSelector:@selector(showViewAnimationDidStop:)];
//    
//    //视图恢复到初始状态
//    _blackView.backgroundColor = [UIColor blackColor];
//    _blackView.alpha = 1.0;
//    _blackView.center = CGPointMake(CGRectGetMidX(_blackView.bounds), CGRectGetMidX(_blackView.bounds));
//    
//    // CGAffineTransformIdentity:移除所有的变幻属性
//    _blackView.transform = CGAffineTransformIdentity;
//    
//    
//    //转场动画(效果)
//    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_blackView cache:NO];
//    
//    //提交动画
//    [UIView commitAnimations];
//}
//
//#pragma mark - UIView动画回调方法
//- (void)showViewAnimationDidStop:(NSString *)animationID{
//    if ([animationID isEqualToString:@"position"]) {
//        [self scaleAnimation];
//    }else if ([animationID isEqualToString:@"scale"]){
//        [self rotationAnimation];
//    }else if ([animationID isEqualToString:@"rotation"]){
//        [self colorAnimation];
//    }else if ([animationID isEqualToString:@"color"]){
//        [self alphAnimation];
//    }else if ([animationID isEqualToString:@"alph"]){
//        [self endAnimation];
//    }
//    
//}

- (IBAction)nextVC:(id)sender {
    ImageAnimationViewController *imageVC = [[ImageAnimationViewController alloc]init];
    [self.navigationController pushViewController:imageVC animated:YES];
}
@end

ImageAnimationViewController.m

#import "ImageAnimationViewController.h"
#define IMAGE_WITH_NAME(name)[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]]
#define SCREEN_WEIGH [UIScreen mainScreen].bounds.size.width

@interface ImageAnimationViewController ()

@property (nonatomic, strong) UIImageView *imageView;

- (void)initUserInterface;

@end

@implementation ImageAnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
    
    
}

- (void)initUserInterface{
    self.view.backgroundColor = [UIColor cyanColor];
    //
    
    NSMutableArray *imageArray = [NSMutableArray array];
    //创建图片数组
    for (int i = 0; i < 138; i ++) {
        NSString *imageName = [NSString stringWithFormat:@"%d",i];
        //第一种
       // UIImage *image = [UIImage imageNamed:imageName];
        //第二种(加载大量的图片)
//        NSString *pathString = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
//        UIImage *image = [UIImage imageWithContentsOfFile:pathString];
        //第三种(宏定义)
        UIImage *image = IMAGE_WITH_NAME(imageName);
         [imageArray addObject:image];
    }
    //创建图片视图
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WEIGH, 400)];
    [self.view addSubview:imageView];
    
    //为其添加图片数组
    imageView.animationImages = imageArray;
    //动画时长
    imageView.animationDuration = 2;
    self.imageView = imageView;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 80, 60);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"stop" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(stopProcess) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
}

- (void)stopProcess{
    [self.imageView stopAnimating];
}

//获取点击屏幕动作
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    // 1. 获取touch
    UITouch *touch = [touches anyObject];
    // 2. 获取当期点
    CGPoint point = [touch locationInView:self.view];
    NSLog(@"%@", NSStringFromCGPoint(point));
     // 3. 动画开始
      [_imageView startAnimating];
}


- (void)viewWillDisappear:(BOOL)animated{
    //animationImages数组无法释放,所以需要手动释放,但是不能直接写 self.catImageView.animationImages = nil;因为动画是异步,还没执行完,数组就释放会崩溃的。所以可以使用performSelector让动画在刚好做完后延迟释放,或者在viewWillDisappear
    
    self.imageView.animationImages = nil;
}

@end


标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值