Ios image.background更改颜色并重复动画
我正在尝试为我的应用程序创建动画。我用我的按钮Ios image.background更改颜色并重复动画,ios,objective-c,animation,Ios,Objective C,Animation,我正在尝试为我的应用程序创建动画。我用我的按钮startButton启动动画。此按钮启动myUIImage的动画并更改其背景色 我想重复这个动画,但我找不到一个方法来做到这一点 - (IBAction)startButton:(id)sender { NSString * chosenColor = _colorField.text; NSString *color = [chosenColor substringToIndex:6]; NSLog(@"colore: %
startButton
启动动画。此按钮启动myUIImage
的动画并更改其背景色
我想重复这个动画,但我找不到一个方法来做到这一点
- (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];
}
}
使用如下所示的NSTimer:
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(doAnimation:)
userInfo:nil
repeats:YES];
每当您希望动画结束时,只需调用[self.animationTimer invalidate]。doAnimation:方法将NSTimer对象作为参数。使用
UIViewAnimationOptionRepeat
选项无限重复UIView动画
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
尝试添加此方法并调用它
-(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];
}
}];
}
并传递计数,如要运行动画的次数
+ (void)animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion
希望这能对你有所帮助。你为什么不试试调用它自己的递归函数呢?我不懂对不起,好吧,告诉我你想重复多少次这个方法,这是你的动画代码。我想重复很多次我在文本字段中写的东西,我在文本字段中转换字符串的计数NSString*count=[chosenColor substringFromIndex:14];整数=[count intValue];我把重复计数放在哪里?它只支持无限计数。否则使用CABASICANIMSTRION。但是使用此方法,我不会用按钮启动动画。您可以在按钮上调用此方法单击iAction方法。我现在不知道如何在iAction中调用此方法…我是developerin count Xcode的新成员。请给我一个错误“变量不可赋值(缺少块类型说明符)