2016-04-06 70 views
-1

我想在滑动时更改视图的颜色,但我不知道如何进行动画颜色更改。我会很感激的帮助。UIView的动画改变颜色

+3

见HTTP://计算器。 com/a/20892927/1226963 – rmaddy

回答

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]; 
}]; 
+0

谢谢!这正是我期待的! – Ookey

+2

为什么不先把简单,简单的解决方案? – rmaddy

+1

但谁会使用过于复杂的解决方案呢? – rmaddy

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"); 
        }]; 
} 
} 
+0

“UIView”(也不是'backgroundView'属性)上没有'setBackgroundView:'这样的方法。 – rmaddy

+0

@rmaddy:编辑答案。谢谢 – Vin