2

I basically have an array of UIView objects that are on screen. They are being moved randomly around and I would like to have a line connecting each object.

In my drawRect method of the UIView that contains all the moving objects, I draw the lines between them. Then once that is done the below method is called for each object

-(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];}];

}

Essentially I would like the lines to stay attached to the objects as they move. If I could call [self setNeedsDisplay]; every time the frame changed at all, then the drawRect method would re-draw the lines, but I can't figure out how to make this happen.

I tried setting an observer on the frame change (shown below) but it only gets triggered once an animation is finished, and doesn't catch the frame changes as the object is mid-animation

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

any body got any ideas??

4

1 回答 1

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);
}
于 2013-03-11T05:12:32.317 回答