2017-09-27 51 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; 
相關問題