iOS Dev (30) 用 beginAnimations 和 commitAnimations 来实现最简单的动画

本文通过一个简单实例介绍如何使用iOS中的beginAnimations和commitAnimations方法来实现视图透明度变化的动画效果,适合初学者快速上手。

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

iOS Dev (30) 用 beginAnimations 和 commitAnimations 来实现最简单的动画

-

说点容易理解的

我发现自己一直很笨,啥玩意儿必须得自己敲他妈一遍才懂。有的3个小时看完一本书,然后就去写代码了。我还是要一点点敲一遍,以前自己吹过牛逼说15分钟就能学一个新语言,扯蛋。

所以,现在还是直接他妈的上代码吧。

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelay:0];
UIView *v = [self.window viewWithTag:1];

// 你想实现到的最终状态

[UIView commitAnimations];

我喜欢带他妈的,就是觉得随便敲而已。放松点,我擦擦。

从:

beginAnimations

到:

commitAnimations

就是完成一次完整的动画。动画,我们自己想想,学过物理的人都知道。首先你要考虑持续时间,其次你要考虑起始时间。也就是 t0 和 Δt 的关系。也就是他俩:

setAnimationDuration
setAnimationDelay

两个方法的参数的量纲(哈哈,这个词儿有点装逼,量纲就是土鳖说的单位),都是秒。

上点完整代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 100, 100)];

    imageView1.image = [UIImage imageNamed:@"gyy.jpg"];
    imageView1.backgroundColor = [UIColor yellowColor];
    imageView1.contentMode = UIViewContentModeScaleToFill;      // 充满
    [self.window addSubview:imageView1];

    imageView1.tag = 1;

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 300, 200, 80)];
    btn.backgroundColor = [UIColor blueColor];
    [btn addTarget:self action:@selector(animate) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)animate
{
    NSLog(@"haha");
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelay:0];
    UIView *v = [self.window viewWithTag:1];
    v.alpha = 0;
    [UIView commitAnimations];
}

-

转载请注明来自:http://blog.youkuaiyun.com/prevention

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值