我想在滑动时更改视图的颜色,但我不知道如何进行动画颜色更改。我会很感激的帮助。UIView的动画改变颜色
-1
A
回答
1
与CALayer合作,如果你搜索它,你可以找到很多简单的例子,例如Ray Wenderlich的这个例子,我发现它很棒。
的UIView
- (void)changeColorWithFillingAnimation {
//we create a view that will increase in size from top to bottom, thats why it starts with these frame parameters
UIView *fillingView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 1, self.view.bounds.size.width, 1)];
//set the color we want to change to
fillingView.backgroundColor = [UIColor blueColor];
//add it to the view we want to change the color
[self.view addSubview:fillingView];
[UIView animateWithDuration:2.0 animations:^{
//this will make so the view animates up, since its animating the frame to the target view's size
fillingView.frame = self.view.bounds;
} completion:^(BOOL finished) {
//set the color we want and then disappear with the filling view.
self.view.backgroundColor = [UIColor blueColor];
[fillingView removeFromSuperview];
}];
}
CAShapeLayer + CABasicAnimation + CATransition
- (void)changeColorWithShapeLayer {
//create the initial and the final path.
UIBezierPath *fromPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, self.view.bounds.size.height - 1, self.view.bounds.size.width, 1)];
UIBezierPath *toPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
//create the shape layer that will be animated
CAShapeLayer *fillingShape = [CAShapeLayer layer];
fillingShape.path = fromPath.CGPath;
fillingShape.fillColor = [UIColor blueColor].CGColor;
//create the animation for the shape layer
CABasicAnimation *animatedFill = [CABasicAnimation animationWithKeyPath:@"path"];
animatedFill.duration = 2.0f;
animatedFill.fromValue = (id)fromPath.CGPath;
animatedFill.toValue = (id)toPath.CGPath;
//using CATransaction like this we can set a completion block
[CATransaction begin];
[CATransaction setCompletionBlock:^{
//when the animation ends, we want to set the proper color itself!
self.view.backgroundColor = [UIColor blueColor];
}];
//add the animation to the shape layer, then add the shape layer as a sublayer on the view changing color
[fillingShape addAnimation:animatedFill forKey:@"path"];
[self.view.layer addSublayer:fillingShape];
[CATransaction commit];
}
一个非常简单的方法来做到这一点是使用一个UIView动画块。
[UIView animateWithDuration:1.0 animations:^{
view.backgroundColor = [UIColor redColor];
}];
2
backgroundColor
是动画开箱
[UIView animateWithDuration:0.5 delay:1.0 options: UIViewAnimationCurveEaseOut animations:^{
//this is the animation block here
myView.backgroundColor = newColor;
} completion:^(BOOL finished) {
if (finished){
NSLog(@"Colour animation complete!");
}
}];
0
Assumming您已实现左轻扫手势,
UISwipeGestureRecognizer * swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];
//实现手势的方法
-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do what you want here
[UIView animateWithDuration:.5
animations:^{
self.view.backgroundColor = [UIColor yellowColor];
}
completion:^(BOOL finished){
NSLog(@"completion block");
}];
}
}
相关问题
- 1. CSS动画移动和改变颜色
- 2. 改变颜色的径向动画
- 3. Android的图标动画改变颜色
- 4. 如何动态改变笔画颜色?
- 5. 在Velocity.js中动画改变颜色
- 6. 改善背景动画颜色变化
- 7. UIView的背景颜色不会改变
- 8. 在动画时更改UIView对象参考的颜色
- 9. 使用Curlup动画时更改uiview的背面颜色
- 10. Xcode:启动画面无颜色改变颜色
- 11. 动画UIView上笔触颜色的变化
- 12. 不断改变UIView背景颜色
- 13. 更改UILabel颜色动画
- 14. jQuery动画颜色变化
- 15. 如何在uiview动画时更改背景颜色
- 16. 改变颜色的颜色
- 17. 苹果改变插画家的颜色
- 18. 如何改变pygal的笔画颜色?
- 19. 改变颜色的颜色改变所有圆形颜色
- 20. 跟踪UIView的框架改变动画
- 21. 的UIView动画接入改变值
- 22. UIView按钮的动画改变图像
- 23. 更改笔划颜色变化以前的笔画颜色
- 24. iPhone - 动态改变颜色
- 25. 改变颜色动态
- 26. 更改matplotlib动画中的RGB颜色
- 27. 更改快门动画的颜色iphone
- 28. 改变颜色
- 29. 改变颜色
- 30. 改变颜色
见HTTP://计算器。 com/a/20892927/1226963 – rmaddy