UI_UIView

本文详细介绍了在iOS应用开发中使用UIKit实现各种动画效果的方法,包括属性动画、过渡动画、关键帧动画等,并展示了如何通过代码控制动画的播放效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  ViewController.m

//  UIView

//

//  Created by HarrySun on 16/7/10.

//  Copyright © 2016   . All rights reserved.

//


#import "ViewController.h"

#import "UIView+MotionEffect.h"

@interface ViewController ()


@property (nonatomic, strong) UIView *myView;


@property (nonatomic, strong) UIView *animationView;


@property (nonatomic, strong) UIImageView *imageView;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // UIView里面还有一些东西,但是还没来得及弄 = = 

    _myView = [[UIView alloc] initWithFrame:self.view.bounds];  // 用来初始化并返回一个新的视图对象,根据指定的CGRect(尺寸)

    

    [self.view addSubview:_myView];

    

    //    _myView = [[UIView alloc] initWithCoder:nil];   // 对于.xib,当你嵌入一个视图对象到xib,视图加载时默认调用的是该方法;例如:假如创建的view来自nib,那么将会调用initWithCoder,由系统来调用,自己不能调用。

    

    NSLog(@"%d",_myView.isUserInteractionEnabled); // 用户交互

    _myView.tag = 101// 标记

    _myView.layer.cornerRadius = 5; // 圆角半径

    _myView.layer.masksToBounds = YES // 是否切除多余部分

    

    

    _myView.frame = CGRectMake(0, 0, 200, 200); // 位置和尺寸(以父控件的左上角为坐标原点(0, 0))

    _myView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);    // 中点(以父控件的左上角为坐标原点(0, 0))

    

    _myView.bounds = CGRectMake(50, 50, 200, 200);  // 位置和尺寸(以自己的左上角为坐标原点(0, 0))

    

    

    _myView.multipleTouchEnabled = YES; // 支持多点触控

    

    

    NSLog(@"_myView父类:%@",_myView.superview);   // 父类

    NSLog(@"子类:%@",_myView.subviews);   // 子类

    

    

    UIView *aView = [[UIView alloc] init];

    //    [self.view addSubview:aView];   // 添加视图

    

    //    [_myView removeFromSuperview];  // 删除视图

    

    

    //    [self.view insertSubview:_myView atIndex:0];    // 插入一个视图

    

    

    [_myView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];    // 更换视图位置

    

    // 添加一个子控件view(被挡在_myView的下面)

    [self.view insertSubview:aView belowSubview:_myView];

    

    // 添加一个子控件view(盖在siblingSubview的上面)

    [self.view insertSubview:aView aboveSubview:_myView];

    

    [self.view bringSubviewToFront:_myView];    // 将某个控件放到顶部

    //    [self.view sendSubviewToBack:_myView];  // 将某个控件放到底部

    

    

    [_myView isDescendantOfView:self.view]; // 是不是view的子控件或者子控件的子控件(是否为view的后代)

    

    UIView *tagView = [self.view viewWithTag:101];  // 通过tag值取得view

    

//    [self.view addSubview:tagView];

    

    tagView.clipsToBounds = YES;    // 超过控件边框范围的内容都剪掉

    tagView.backgroundColor = [UIColor redColor];   // 背景颜色

    tagView.alpha = 0.5;    // 透明度

    tagView.hidden = NO;    // yes:隐藏 no:显示

    tagView.opaque = YES;   // 是否不是透明的(yes是不透明,no是透明)

    

    

    tagView.layer.borderWidth = 1//设置描边宽度

    tagView.layer.borderColor = [UIColor blueColor].CGColor;    // 设置描边的颜色(CALayer上用的是CGColor)

    tagView.layer.shadowOffset = CGSizeMake(50, 100);   // 阴影偏移量,width影响水平(正右负左),height影响垂直(正上负下)

    tagView.layer.shadowColor = [UIColor grayColor].CGColor;    //阴影偏移的颜色

    tagView.layer.shadowOpacity = 1;    // 阴影的不透明度(默认是0,就是透明的)

    

    

    // 属性动画

    //    [self pressPropertyAnimation];

    

    // 过渡动画

    //    [self pressTranstionAnimation];

    

    _animationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

    _animationView.backgroundColor = [UIColor redColor];

        [self.view addSubview:_animationView];

    

    // 关键帧动画

        [self pressKeyFrameAnimation];

    

    

    

    // 添加手势

    [_animationView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]];

    

    

    

    /*

     

     _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];

     _imageView.center = CGPointMake(200, self.view.bounds.size.height - 200);

     [self.view addSubview:_imageView];

     

     _imageView.effectGroup = [UIMotionEffectGroup new];

     [_imageView addXAxisWithValue:5.f YAxisWithValue:5.f];

     

     */

    

    

    

    

    // autolayout

    

}



- (void)tap:(UITapGestureRecognizer *)sinder{

    

    

    

    

    NSLog(@"tap");

    // 删除手势

    [_animationView removeGestureRecognizer:sinder];

}



// 属性动画

- (void)pressPropertyAnimation{

    

    [UIView beginAnimations:@"改变大小" context:NULL];  // 标记动画块开始

    [UIView setAnimationDuration:5.0f]; // 设置动画持续时间

    [UIView setAnimationDelegate:self];

    //    [UIView setAnimationDelay:1]; // 动画延迟执行时间

    [UIView setAnimationWillStartSelector:@selector(startAnimation)];

    

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];   // 定义动画的曲线

    [UIView setAnimationRepeatCount:20];    // 动画的重复次数

    [UIView setAnimationRepeatAutoreverses:YES];    // 动画往返执行,必须设置动画的重复次数

    

    _myView.backgroundColor = [UIColor colorWithRed:( arc4random() % 256 / 256.0 ) green:( arc4random() % 256 / 256.0 ) blue:( arc4random() % 256 / 256.0 ) alpha:1];

    

    [UIView commitAnimations];  // 提交并执行动画

}


// 过渡动画

- (void)pressTranstionAnimation{

    

    // 准备动画

    [UIView beginAnimations:@"过渡动画" context:NULL];

    [UIView setAnimationDuration:5];

    [UIView setAnimationRepeatCount:50];

    

    // 设置过渡样式

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.myView cache:YES];

    self.myView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];

    

    

    [UIView commitAnimations];

}




// block动画

- (void)pressBlockAnimation{

    

    // 动画时长 block

    //    [UIView animateWithDuration:2 animations:^{

    //        self.myView.backgroundColor = [UIColor orangeColor];

    //    }];

    

    

    

    [UIView animateWithDuration:2 animations:^{

        self.myView.backgroundColor = [UIColor orangeColor];

    } completion:^(BOOL finished) {

        if (finished) { // 判断动画是否完成

            NSLog(@"动画完成");

        }

    }];

    

    

}


// 关键帧动画

- (void)pressKeyFrameAnimation{

    

    

    // 创建动画

    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];

    [keyFrame setDuration:10];

    // 修改属性

    keyFrame.values = @[(id)[UIColor redColor].CGColor, (id)[UIColor orangeColor].CGColor, (id)[UIColor yellowColor].CGColor, (id)[UIColor greenColor].CGColor, (id)[UIColor blueColor].CGColor];

    // keyTime代表了出现动画的时刻,值必须是递增的

    keyFrame.keyTimes = @[@(0.3), @(0.5), @(0.6), @(0.7), @(0.9)];

    [self.animationView.layer addAnimation:keyFrame forKey:nil];

}






#pragma mark - AnimationDelegate

//动画将要开始时调用

- (void)animationWillStart:(NSString *)animationID context:(void *)context{

    

    NSLog(@"start: %@, %@", animationID, context);

}


//动画结束时调用

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{

    

    NSLog(@"stop: %@, %@", animationID, context);

}





- (void)startAnimation{

    

    NSLog(@"动画开始");

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值