2017-09-27 53 views
0

我有代码来绘制矩形作为如何不间断的改变矩形的高度在iOS应用

- (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

尝试通过核心动画做到这一点:

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; 
相关问题