我有 10 张图片,我需要每 4 秒随机更改一次背景颜色,并具有流畅的动画效果。
我的代码是
self.bgTimer = [NSTimer scheduledTimerWithTimeInterval: 4.0 target: self
selector: @selector(updateViewBackground)
userInfo: nil repeats: YES];
self.bgTimer
是 NSTimer
属性。
而改变背景的方法是
- (void)updateViewBackground {
int randomNumber = arc4random_uniform(10);
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat"random_bg_%i",randomNumber]]];
}
它根据选择的随机图像改变背景,但它突然改变了背景。
我需要平滑地改变颜色(需要 2 秒才能像淡入淡出或交叉溶解那样改变)。
如何做到这一点。
还有没有更好的方法来代替这个 NSTimer
方法。
用这个试试..
- (void)updateViewBackground {
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFromBottom];
[animation setDuration:1.0];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.view layer] addAnimation:animation forKey"transitionViewAnimation"];
int randomNumber = arc4random_uniform(10);
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat"random_bg_%i",randomNumber]]];
}
或
- (void)updateViewBackground {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
int randomNumber = arc4random_uniform(10);
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat"random_bg_%i",randomNumber]]];
[UIView commitAnimations];
}
关于iphone - 如何平滑更改 View 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17989849/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |