Objective-C动画一:View动画

本文介绍了Objective-C中实现UIView动画的方法,包括UIView动画块、动画block语法、UIViewAnimationOptions选项,特别是详细讲解了如何使用AffinaTransFrom进行2D和3D仿射变换,如旋转、缩放和平移,并通过实例展示了3D仿射变换在抢红包动画的应用。

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

我们定义一个UIView属性view4animation来执行动画:

1. UIView动画块

//标识着动画的开始
    [UIView beginAnimations:@"btn4kuai" context:nil];

    //这里面写动画的内容
    //动画持续时间
    [UIView setAnimationDuration:1];
    //改变颜色
    self.view4animation.backgroundColor = [UIColor blueColor];

    CGPoint center = self.view4animation.center;
    center.x += 100;
    center.y += 120;
    //改变中心点
    self.view4animation.center = center;
    //改变透明度
    self.view4animation.alpha = 1;

    //动画结束时执行事件
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(changeBtn:)];

    //提交动画,也就是动画结束了
    [UIView commitAnimations];
- (void)changeBtn:(NSString *)str{

    NSLog(@"view动画执行完毕");
    [UIView beginAnimations:@"btn" context:nil];

    //设置反复次数
    //必须放在动画属性代码前面面
    [UIView setAnimationRepeatCount:3];
    [UIView setAnimationRepeatAutoreverses:YES];

    [UIView setAnimationDuration:1];
    CGPoint center = self.view4animation.center;
    center.x -=  100;
    self.view4animation.center = center;
    [UIView commitAnimations];
}

2. 动画block语法

//参数1.动画时间 2.动画效果
    [UIView animateWithDuration:1 animations:^{
        //这写动画效果
        self.view4animation.backgroundColor = [UIColor yellowColor];
        self.view4animation.alpha = 0.3;
    }];
    [UIView animateWithDuration:2 animations:^{

        //这写动画
        CGPoint p = self.view4animation.center;
        p.x += 200;
        p.y += 200;
        self.view4animation.center = p;

    } completion:^(BOOL finished) {

        //这是动画执行完之后在执行的,可以在这继续加动画
        //返回动画
        [UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
            CGPoint p = self.view4animation.center;
            p.x -= 200;
            p.y -= 200;
            self.view4animation.center = p;

        } completion:^(BOOL finished) {

        }];
    }];

3 UIViewAnimationOptions动画

UIViewAnimationOptions是系统的一些例如翻转移出之类的动画,这里介绍一下翻页的效果:

UIView *newView = [[UIView alloc]initWithFrame:self.view.frame];
    newView.tag = 100;
    newView.backgroundColor = [UIColor blueColor];

    //第一个参数是在哪个视图上进行翻页效果
    //第二个参数是翻页持续时间
    //第三个参数是翻页效果
    [UIView transitionWithView:self.view.superview duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{

        [self.view addSubview:newView];

    } completion:^(BOOL finished) {
        //执行结束
    }];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [newView addGestureRecognizer:singleTap];
- (void)singleTap:(UITapGestureRecognizer*)gesture{
    [UIView transitionWithView:self.view.superview duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        UIView *view = (UIView *)[self.view viewWithTag:100];
        [view removeFromSuperview];

    } completion:^(BOOL finished) {
        //执行结束
    }];
}

4. AffinaTransFrom动画

AffinaTransFrom动画常用的操作有偏移,缩放,旋转等操作,它只进行变换,并不带动画效果,需要与其他动画或者timer相配合使用。AffinaTransFrom分为2D仿射变换和3D仿射变换。顾名思义,2D仿射变换是在x轴和y轴上进行变换,3D仿射变换则引入了z轴,即与屏幕垂直的方向。
2D仿射变换旋转、缩放、平移:

[UIView animateWithDuration:1 animations:^{
        //旋转
        self.view4animation.transform = CGAffineTransformMakeRotation(M_PI);
        //缩放参数是原来的多少倍,是一个比例值
        self.view4animation.transform = CGAffineTransformMakeScale(0.5, 0.5);
        //偏移
        self.view4animation.transform = CGAffineTransformMakeTranslation(100, 100);
    } completion:^(BOOL finished) {

        [UIView animateWithDuration:1 animations:^{

            //1.创建一个仿射变换
            CGAffineTransform form = CGAffineTransformIdentity;
            //2.向仿射变换里添加要做的操作
            CGAffineTransformRotate(form, M_PI);
            CGAffineTransformScale(form, 0.5, 0.5);
            CGAffineTransformTranslate(form, -100, -100);
            //3.将我们的仿射变换对象给我们的控件
            self.view4animation.transform = form;

            //这时就会有一个组合的效果了
        }];
    }];

3D仿射变换我们来做一个抢红包的动画,即旋转:
定义一个属性int属性:

@property (nonatomic, assign) int rotateNum;
_rotateNum = 1
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(starAnimate) userInfo:nil repeats:YES];
- (void)starOPenAnimate{
    _openAnimateView.layer.transform = CATransform3DMakeRotation(M_PI/5*(_rotateNum++), 0, 1, 0);
}

至于CATransform3D的参数说明,这篇博客讲得很清楚:http://blog.sina.com.cn/s/blog_51a995b70101mz3q.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值