用drawRect UIViewanimationbackgroundColor:

我有一个观点,能够自我反思。 这实际上是UICollectionView一个子类,但我只是在与UIView特定的东西挣扎; backgroundColor。

我只是添加了一个UIPanGestureRecognizer ,保存起始点在UIGestureRecognizerStateBegan和终点在UIGestureRecognizerStateChanged 。 然后我使用-drawRect:方法来绘制实际的path:

 - (void)awakeFromNib { UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; [self addGestureRecognizer:pan]; } - (void)panGesture:(UIPanGestureRecognizer *)panRecon { if([panRecon state] == UIGestureRecognizerBegan) { startPoint = [panRecon locationInView:self]; } else if([panRecon state] == UIGestureRecognizerChanged) { endPoint = [panRecon locationInView:self]; [self setNeedsDisplay]; } else if([panRecon state] == UIGestureRecognizerEnded /* || failed || cancelled */) { startPoint = CGPointZero; endPoint = CGPointZero; [self setNeedsDisplay]; } } - (void)drawRect:(CGRect)rect { if(!CGPointEqualToPoint(startPoint, CGPointZero) && !CGPointEqualToPoint(endPoint, CGPointZero)) { CGRect selectionRect = CGRectMake(startPoint.x, startPoint.y, endPoint.x - startPoint.x, endPoint.y - startPoint.y); [[UIColor colorWithWhite:1.0 alpha:0.3] setFill]; [[UIColor colorWithWhite:1.0 alpha:1.0] setStroke]; CGContextFillRect(UIGraphicsGetCurrentContext(), selectionRect); CGContextStrokeRect(UIGraphicsGetCurrentContext(), selectionRect); } } 

我现在想要在视图闪烁的情况下启动select模式(实际上是矩形)。 我为此创build了一个UIViewanimation:

 - (void)startSelection { [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ [self setBackgroundColor:[UIColor whiteColor]]; } completion:^(BOOL finished){ if(finished) { [UIView animateWithDuration:0.9 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self setBackgroundColor:[UIColor blackColor]]; } completion:nil]; } } } 

问题是:一旦我实现了-drawRect:方法,UIView不再animationbackgroundColor的变化了。 我已经尝试过UIViewAnimationOptionAllowAnimatedContent和几乎所有我能find的谷歌,但我无法解决我的问题。

有谁知道我可以如何animationUIView backgroundColor,并有-drawRect:实施?