2013-03-11 92 views
2

我基本上有一個屏幕上的UIView對象數組。他們被隨機移動,我想有一條線連接每個對象。iOS:如何在兩個移動的對象之間繪製一條線?

在我包含所有移動物體的UIView的drawRect方法中,我畫了它們之間的界線。然後,一旦做到這一點下面的方法被調用爲每個對象

-(void)animateIcon:(Icon*)icon{ 
[UIView animateWithDuration:(arc4random() % 100 * 0.1) 
         delay: 0.0 
        options: UIViewAnimationOptionCurveEaseIn 
       animations:^{ 
        icon.frame = CGRectMake((arc4random() % 320), (arc4random() % ((int)self.frame.size.height - 70)), 52, 52); 
       } 
       completion:^(BOOL finished){[self animateIcon:icon];}]; 

}

基本上我想的線保持連接的對象,因爲他們移動。如果我可以調用[self setNeedsDisplay];每次框架都改變了,那麼drawRect方法會重新繪製線條,但我無法弄清楚如何實現這一點。

我嘗試設置在框架變化(如下所示)的觀察者,但它只有一次的動畫完成被觸發,並且當對象是中期動畫沒有趕上幀變化

[icon addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:NULL]; 

任何機構有任何想法?

回答

0

把一個陣列中的所有移動視圖

[UIView setAnimationDidStopSelector:@selector(animationStopped:isFinished:context:)]; 


- (void)animationStopped:(NSString*)animationID isFinished:(BOOL)finished context:(void *)context 
{ 
    context = UIGraphicsGetCurrentContext() ; 
    CGContextSaveGState(context); 
    CGContextSetStrokeColorWithColor(context,[UIColor blueColor].CGColor); 
    CGContextSetLineWidth(myContext, 5.0); 
    CGMutablePathRef myPathRef = CGPathCreateMutable() ; 
    for (int ind = 0 ; ind < [movingViewArray count] ; ind++) 
     { 
      UIView *tmpView=[movingViewArray objectAtIndex:ind]; 
      CGPoint point=tmpView.frame.center; 
     if(ind==0) CGPathMoveToPoint(myPathRef, nil, point.x, point.y); 
      else { 
       CGPathAddLineToPoint(myPathRef, nil, point.x, point.y); 
       CGPathMoveToPoint(myPathRef, nil, point.x, point.y); 
       } 
     } 

     CGContextAddPath(context, myPathRef) ; 

     CGPathRelease(myPathRef); 

     CGContextDrawPath(context,kCGPathStroke); 
     CGContextClip(context); 
} 
相關問題