2013-03-11 89 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); 
} 
相关问题