UIView animated backgroundColor with drawRect:

I have a view that can draw a straight line. This is actually a subclass of UICollectionView , but I'm just struggling with specific UIView things; backgroundColor.

I just added a UIPanGestureRecognizer , saved the start point in UIGestureRecognizerStateBegan and the end point in UIGestureRecognizerStateChanged . Then I use the -drawRect: method to draw the actual 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); } } 

Now I want to start the selection mode (which should actually be) with the view blinking. I created a UIView animation for this:

 - (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]; } } } 

The problem is this: as soon as I implement the -drawRect: method, the UIView no longer changes the backgroundColor change. I already tried UIViewAnimationOptionAllowAnimatedContent and almost everything I could find on google, but I could not solve my problem.

Does anyone know how I can animate backgroundColor UIView and do -drawRect: :?

+3
source share

Source: https://habr.com/ru/post/983373/


All Articles