我想在刷卡时改变视图的颜色,但我不知道如何进行动画颜色更改。我很感激你的帮助。
答案 0 :(得分: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!");
}
}];
答案 1 :(得分: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 :(得分:0)
假设您已实施左滑动手势,
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");
}];
}
}