我正在尝试为我的应用程序创建动画。我用我的按钮startButton
开始动画。此按钮启动我的UIImage
动画并更改其背景颜色。
我想重复这个动画,但我找不到这样做的方法。
- (IBAction)startButton:(id)sender {
NSString * chosenColor = _colorField.text;
NSString *color = [chosenColor substringToIndex:6];
NSLog(@"colore: %@", color);
NSString *color1 = [chosenColor substringFromIndex:8];
NSLog(@"colore2: %@", color1);
NSScanner* pScanner = [NSScanner scannerWithString: color];
NSScanner * pScanner2 = [NSScanner scannerWithString:color1];
unsigned int rgbValue = 0;
unsigned int rgbValue2 = 0;
[pScanner scanHexInt: &rgbValue];
[pScanner2 scanHexInt:&rgbValue2];
int index = 8;
NSString *time = [NSString stringWithFormat:@"%c", [chosenColor characterAtIndex:index-2]];
NSLog(@"tempo: %@", time);
NSTimeInterval timeInterval = [time doubleValue];
NSLog(@"interval: %f", timeInterval);
_colorImage.backgroundColor = [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0];
[UIView animateWithDuration:timeInterval animations:^{
_colorImage.backgroundColor = [UIColor colorWithRed:((float)((rgbValue2 & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue2 & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue2 & 0xFF))/255.0 alpha:1.0];
} completion:NULL];
}
}
答案 0 :(得分:0)
使用像这样的NSTimer:
new Promise(…).then(…);
每当你想要动画结束时,只需调用[self.animationTimer invalidate]。 doAnimation:方法将NSTimer对象作为参数。
答案 1 :(得分:0)
尝试添加此方法调用它。
-(void)runAnimationWithRepeateCount:(int)count
{
_colorImage.backgroundColor = [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0];
[UIView animateWithDuration:5 animations:^{
_colorImage.backgroundColor = [UIColor colorWithRed:((float)((rgbValue2 & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue2 & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue2 & 0xFF))/255.0 alpha:1.0];
} completion:^(BOOL finished) {
count = count-1;
if (count>0) {
[self runAnimationWithRepeateCount:count];
}
}];
}
并将计数传递给您想要运行动画的次数。
希望这会对你有所帮助。
答案 2 :(得分:-1)
使用UIViewAnimationOptionRepeat
选项无限重复UIView动画。
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion