3

I have a view that is able to draw a rect on itself. It is actually a subclass of UICollectionView but I'm just struggling with UIView specific stuff; the backgroundColor.

I simply added a UIPanGestureRecognizer, saved the start point at UIGestureRecognizerStateBegan and the end point at UIGestureRecognizerStateChanged. I then 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);
    }
}

I now want to start the selection mode (which the rect should actually be) with the view flashing. I created a UIView animation for that:

- (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: once I implement the -drawRect: method, UIView does not animate the backgroundColor change anymore. I already tried UIViewAnimationOptionAllowAnimatedContent and almost everything I could find on google, but I wasn't able to solve my problem.

Does anybody know how I can animate backgroundColor of an UIView and have -drawRect: implemented?

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
  • I had the same problem, but this helped me through ^^ http://stackoverflow.com/questions/10809280/drawrect-circle-and-animate-size-color – Durican Radu Nov 25 '13 at 16:09
  • @user2277872 this is not true. If you read my question again, you will see that I am creating a subclass of `UIView`, not `UIViewController`. `UIView` has no property or outlet named `view`. The view is editing itself. @Durican Radu: thanks, I will look at it later. – Julian F. Weinert Nov 25 '13 at 17:20
  • ah, yes it does. My mistake. I deleted the comment; thank you for pointing that out to me – user2277872 Nov 28 '13 at 02:59

0 Answers0