我有代码将矩形绘制为
- (void)drawRect:(CGRect)frame {
UIBezierPath* rectanglePath = [UIBezierPath
bezierPathWithRect:CGRectMake(67, 50, 2, _height)];
[UIColor.grayColor setFill];
[rectanglePath fill];
}
其中_height值将在点击
时连续变化我有这样的代码
- (void)initialize {
self.userInteractionEnabled = YES;
_displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(redrawView:)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop]forMode:NSDefaultRunLoopMode];
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObjectsFromArray:@[@“10”,@“20”,@“30”,@“40”]];
}
- (void)dealloc {
[_displayLink invalidate];
[_displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
_displayLink = nil;
}
- (void)setInteractionState:(InteractionState)interactionState {
if (_interactionState == Idle && interactionState != Idle) {
_displayLink.paused = NO;
}
_interactionState = interactionState;
}
- (void)redrawView:(CADisplayLink *)displayLink {
if (_interactionState == start) {
_height = [array firstObject];
[array removeObjectAtIndex:0];
[array addObject:_height];
}
}
如何在互动状态播放时更改高度?
答案 0 :(得分:0)
尝试通过Core Animation执行此操作:
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(67, 50, 2, _height) cornerRadius:0.0];
shapeLayer.strokeColor = [[UIColor blueColor] CGColor];
shapeLayer.lineWidth = 3.0;
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
[self.view.layer addSublayer:shapeLayer];
UIBezierPath *newPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(67, 50, 2, _height) cornerRadius:0.0];
CABasicAnimation* pathAnim = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnim.fromValue = (id)self.shapeLayer.path;
pathAnim.toValue = (id)newPath.CGPath;
pathAnim.duration = 2.0f;
[self.circle addAnimation:pathAnim forKey:@"redraw"];
self.shapeLayer.path = newPath.CGPath;
答案 1 :(得分:0)
UIView
类中有两种方法可以重绘视图
- (void)setNeedsDisplay;
重绘指定区域
例如
- (void)changeHeightTo:(CGFloat)height {
_height = height;
[self setNeedsDisplay]; //this method will notify the system
//to call `drawRect` in next drawing cycle
}