同步UIImageView动画

时间:2012-08-30 12:33:08

标签: ios animation uiimageview uibutton

我在UIScrollView上有三个键盘,每个键盘都是一列按钮。当我获得按钮状态的更新(键盘充当代表)时,我有一些响应此状态更改的按钮图像 - On,Off和两个On / Off动画(1 Hz和10 Hz)。现在我已经完成了所有工作,但我需要正确地同步所有动画。代码我在单个键盘上同步动画但不是页面上的所有键盘(这是在KeypadController中):

#pragma mark -
#pragma mark LEDDelegate

- (void)LEDUpdated:(LED *)sender {
    // Search through our LEDs for a match on the updating LED. This gives us
    // the button index so we can update properly.
    int ledIndex = -1; // Needs to be zero indexed
    Component *tempComp;
    LED *tempLED;
    for (NSUInteger itemCount = 0; itemCount < [taskButtonTitleCollection count]; ++itemCount) {
        tempComp = (Component *) [taskButtonTitleCollection objectAtIndex:itemCount];
        if ([[tempComp getComponentType] isEqualToString:@"LED" ]){
            tempLED = (LED *) [tempComp getComponentElement];
            ledIndex++;
            if (tempLED == sender) {
                break;
            }
        }
    }

    //Code for selecting images here - removed for brevity    

    // Declare the image we're about to use
    UIImage *newNormal, *newHighlight, *newBlink;

    // Going to need the button for flashing
    UIButton *btn = [buttonCollection objectAtIndex:ledIndex];

    // For syncing animation, part 1
    // TODO: This syncs a single keypad, which means we don't sync between columns
    NSMutableArray *buttonsToSync = [[NSMutableArray alloc] init];
    for (UIButton *active in buttonCollection) {
        if ([[active imageView] isAnimating]) {
            [[active imageView] stopAnimating];
            if (active != btn) {
                [buttonsToSync addObject:active];
            }
        }  
    }

    // Grab the updated state of the current led object so we can select the
    // correct button images
    switch ([tempLED state]) {
        case LED_STATE_ON:
            newNormal = [[Utils getImageNamed:pressedImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];

            newHighlight = [[Utils getImageNamed:highlightedImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
            break;

        case LED_STATE_FLASH1: // 1 Hz blinking
            newNormal = [[Utils getImageNamed:normalImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
            newBlink = [[Utils getImageNamed:pressedImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];

            // We can animate between these two images at the correct
            // speed with the below code.
            [[btn imageView] setAnimationImages:[NSArray arrayWithObjects:newNormal, newBlink, nil]];
            [[btn imageView] setAnimationDuration:1];
            [buttonsToSync addObject:btn];

            newHighlight = [[Utils getImageNamed:highlightedImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
            break;

        case LED_STATE_FLASH2: // 10 Hz blinking
            newNormal = [[Utils getImageNamed:normalImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
            newBlink = [[Utils getImageNamed:pressedImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];

            // We can animate between these two images at the correct
            // speed with the below code.
            [[btn imageView] setAnimationImages:[NSArray arrayWithObjects:newNormal, newBlink, nil]];
            [[btn imageView] setAnimationDuration:0.1];
            [buttonsToSync addObject:btn];

            newHighlight = [[Utils getImageNamed:highlightedImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
            break;

        case LED_STATE_OFF:
        default:
            newNormal = [[Utils getImageNamed:normalImage] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];

            newHighlight = [[Utils getImageNamed:normalImageWithoutShadow] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
            break;
    }

    // Set the new button images for the normal and highlighted states
    [btn setImage:newNormal forState:UIControlStateNormal];
    [btn setImage:newHighlight forState:UIControlStateHighlighted];
    newNormal = nil, newBlink = nil, newHighlight = nil;

    // For syncing animation, part 2
    // TODO: This syncs a single keypad, which means we don't sync between columns
    for (UIButton *active in buttonsToSync) {
        [[active imageView] startAnimating];
    }       
}

同样,这会同步一个键盘的动画但不是全部。

我提出的一个可能的解决方案是让所有动画从系统时间的某个倍数开始(例如,尽可能接近一秒),但我无法想出一种可行的方法那。我尝试在同步的第2部分(我重新启动动画)之前添加它:

NSDate *date = [NSDate date];
double timePassed_ms = [date timeIntervalSince1970] * 1000.0;

while ( (int)timePassed_ms % 500 != 0) {
    timePassed_ms = [date timeIntervalSince1970] * 1000.0;
}

不幸的是,这不起作用(无限循环和阻止GUI)。我猜这是因为我从未在恰当的时间进行过投票。

有办法做我想做的事吗?

0 个答案:

没有答案