300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > uiview的animation动画

uiview的animation动画

时间:2022-10-12 19:11:39

相关推荐

uiview的animation动画

设置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];也可以让动画重复,但是两者不同,前者是红渐变到蓝,再由蓝渐变到红,后者是由红渐变到蓝,然后突然变红,再渐变到蓝。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。