我有一个能够在自己上画一个矩形的视图。
它实际上是UICollectionView
的子类,但我只是在努力解决UIView特定的问题; backgroundColor。
我只是添加了UIPanGestureRecognizer
,将起点保存在UIGestureRecognizerStateBegan
,结束点保存在UIGestureRecognizerStateChanged
。然后我使用-drawRect:
方法绘制实际路径:
- (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);
}
}
我现在想要在视图闪烁的情况下启动选择模式(rect应该是实际的)。我为此创建了一个UIView
动画:
- (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就不再为backgroundColor更改设置动画了。我已经尝试了UIViewAnimationOptionAllowAnimatedContent
几乎我在谷歌上找到的所有内容,但我无法解决我的问题。
是否有人知道如何设置UIView
的backgroundColor动画并实现-drawRect:
?