Reputation: 251
Dose anybody know how to animate text blinking in UITextView? Regarding to blinking animation, I would like to use couple of colour variations in NSArray,,,
This is my current methods for it:
- (void)startFontColorFlashing:(UITextView *)textView{
[UIView animateKeyframesWithDuration:2.0
delay:0.0
options:UIViewKeyframeAnimationOptionRepeat | UIViewKeyframeAnimationOptionCalculationModeCubic
animations:^{
NSLog(@"Font flash animation start");
NSArray *fontFlashColorsArr = @[[UIColor redColor], [UIColor blueColor]];
NSUInteger colorCount = [fontFlashColorsArr count];
for (NSUInteger i = 0; i < colorCount; i++) {
[UIView addKeyframeWithRelativeStartTime:i/(CGFloat)colorCount
relativeDuration:1/(CGFloat)colorCount
animations:^{
textView.textColor = fontFlashColorsArr[i];
}];
}
}
completion:^(BOOL finished){
NSLog(@"Finished the animation! %d", finished);
}];
}
It should works, but as soon as staring the animation it will be finished. Please give me a good tip!
Cheers,
Upvotes: 2
Views: 595
Reputation: 3971
I experienced difficulty attempting to animate the text color changing, just like you did. I'm not sure if this is the best solution, but it works for what you are trying to do. Just add a way to stop changeTextColor from being called an infinite number of times, like a global variable, and this should do the trick
- (void)textViewDidBeginEditing:(UITextView *)textView {
[self changeTextColor:textView];
}
- (void)changeTextColor:(UITextView *)textView {
[self performSelector:@selector(changeTextColor:) withObject:textView afterDelay:0.3];
if (textView.textColor == [UIColor redColor]) {
textView.textColor = [UIColor blueColor];
} else {
textView.textColor = [UIColor redColor];
}
}
Here is some updated code to use multiple colors. This assumes self.count is a global variable that is handling the number of times to blink. This is probably best done in a subclass of UITextView if you are using more than one in the same controller.
- (NSArray *)colors {
return @[[UIColor redColor], [UIColor blueColor], [UIColor greenColor]];
}
- (void)changeTextColor:(UITextView *)textView {
if (self.count < 20) {
[self performSelector:@selector(changeTextColor:) withObject:textView afterDelay:0.3];
} else {
self.count = 0;
return;
}
NSInteger colorIndex = self.count % 3;
self.textView.textColor = self.colors[colorIndex];
self.count++;
}
Upvotes: 1