uiview的animation动画

本文介绍了一个使用UIKit实现的简单视图动画示例。通过创建两个颜色不同的UIView,并使用UIButton触发动画,实现视图间的平滑切换。代码展示了如何设置动画时长、过渡效果及动画曲线等。

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

设置uiview简单的动画

新建一个single view工程,在ViewController中添加两个view和一个button,两个view颜色不同,用button来控制两个view切换

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
    UIView *view2= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    view2.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view2];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 100, 40);
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

}

设置button click事件

- (void)buttonClick
{
    [UIView beginAnimations:nil context:nil];//开始

    [UIView setAnimationDuration:20.0];//动画时长
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];//第一个参数是动画效果,第二个是哪个视图进行动画,第三个是缓存
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置渐变效果
    
    [UIView setAnimationDelegate:self];//设置代理
    [UIView setAnimationWillStartSelector:@selector(willStartAnimation)];
    
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    
    [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];

    [UIView commitAnimations];//提交动画
}
其中setAnimationDidStopSelector和setAnimationWillStartSelector可以设置动画结束和开始的时候调用的事件,比如我们可以将结束时的选择器设置为@selector(buttonClick),这样动画就会循环;当然[UIView setAnimationRepeatCount:3];也可以让动画重复,但是两者不同,前者是红渐变到蓝,再由蓝渐变到红,后者是由红渐变到蓝,然后突然变红,再渐变到蓝。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值