2014-06-26 52 views
1

我想用Blink动画更改我的UILabel文本。文本应该是黑色的“text1”,消失,然后改为“text2”和红色,反之亦然。 这是我的代码UIView动画自动反向和重复不起作用

#import "ViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UIImageView *image; 
@property (weak, nonatomic) IBOutlet UILabel *label; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [self performSelector:@selector(combineAnimations) withObject:self afterDelay:0.0]; 
    self.label.textColor = [UIColor blackColor]; 
    self.label.text = @"text1"; 

    [self animateView]; 
} 

- (void) animateView { 
    [UIView animateWithDuration:2.0 
          delay:0.0 
         options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat |UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
         self.label.text = @"text2"; 
         self.label.textColor = [UIColor redColor]; 
        } 
        completion:nil]; 
} 

我在想什么?

+0

你目前的代码? –

+0

第二个文本与前一个文本重叠一段时间,然后再次变回相同的效果。 –

回答

-1

使用在完成块中互相调用的两种方法。你也可以调整一个快速淡入或淡出......这比一个简单的“眨眼”不那么震撼。如果你想眨眼间,没有时间,只需将延迟设置为0.0。同样,您可以在其中调整Alpha以获得淡入淡出效果。

- (void) animateView { 

self.label.text = @"text2"; 
        self.label.textColor = [UIColor redColor]; 
[UIView animateWithDuration:.5 
         delay:1.0 
        option:UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        self.label.alpha = 1; 
       } 
       completion:{ 
     [self animateViewReverse]; 
}]; 
} 

- (void) animateViewReverse { 

self.label.text = @"text1"; 
self.label.textColor = [UIColor blackColor]; 
[UIView animateWithDuration:1.0 
         delay:1.0 
        option:UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 
        self.label.alpha = 1; 

       } 
       completion:{ 
      [self animateView]; 
}]; 
} 

你似乎有点困惑的一件事是动画标签的文字。你实际上并没有对文本字符串进行动画处理,只是像alpha,size,position这样的东西。因此,对于您的情况,您可以更改动画之前的颜色和文本字符串,只需使用动画作为触发下一个动画/反转的方式 - 稍后还会为更细致的动画构建功能。

在你想在所有这样做没有动画,你可以简单地创建两个方法,每个设置文本的一种方式,然后调用其他像延迟:视觉效果所生产的是什么

[self performSelector:@selector(animationReverse) withObject:nil afterDelay:2]; 
+0

它不起作用。 –

+0

它可以工作或不工作?如果你想提供一些细节,我相信我们可以为你提供你所需要的。 – Fluffhead