2010-11-09 85 views
1

莫斯科晚上给大家!我还是不熟悉iPhone动画的原理(顺便说一句,有没有人知道一个关于这个大而好的教程?),但在我的项目中,我想做的按钮“突出显示,不突出显示”闪烁通知用户它的标签已经改变。动画选择UIButton

此代码不会做任何事情(它只是闪烁的动画片段):

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration: 0.5]; 

[button setHighlighted: YES]; 

[UIView commitAnimations]; 

而这其中凸显的按钮,但不这样做的动画形式:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration: 0.5]; 

[button setSelected: YES]; 

[UIView commitAnimations]; 

任何人都可以帮助我说:

  1. 这段代码有什么问题?
  2. 什么可以解决这个问题?

-----------------------------------更新-------- ----------------------

我想那样的代码,但它无法正常工作或:

// ------------------------ 
// --- animation ---------- 
// ------------------------ 

- (void)animateIn 
{ 
[UIView beginAnimations: @"animateIn" context:nil]; 
[UIView setAnimationDuration: 0.2]; 

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

[control setBackgroundColor:[UIColor blackColor]]; 

[UIView commitAnimations]; 
} 

- (void)animateOut 
{ 
[UIView beginAnimations: @"animateOut" context:nil];  
[UIView setAnimationDuration: 0.2]; 

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

[control setBackgroundColor:[UIColor whiteColor]]; 

[UIView commitAnimations]; 
} 


- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
if([animationID isEqualToString: @"animateIn"]) 
{ 
    [self animateOut]; 
    return; 
} 
else if ([animationID isEqualToString: @"animateOut"]) 
{ 
    cycleCount++; 

    if(cycleCount < 3) 
     [self animateIn]; 
    else 
     cycleCount = 0; 

    return; 
} 
} 

@end 

回答

2
//isFlickerOn is declared in .h file as BOOL isFlickerOn; 
//flickerTimer is declared in .h file as NSTimer *flickerTimer; 
//flickerCount is declared in .h file as int flickerCount; 
//control is a UILabel, UIButton background color is really hard to notice 
//especially the roundedRect UIButton, so I just flickered a UILabel's textColor 

-(void)flickerOn { 
    if (flickerCount < 5) 
    { 
    flickerCount++; 
     if (!isFlickerOn) 
     { 
      [control setTextColor:[UIColor yellowColor]]; 
      isFlickerOn = YES; 
     } 
     else 
     { 
      [control setTextColor:[UIColor blueColor]]; 
      isFlickerOn = NO; 
     } 
    } 
    else 
    { 
     [flickerTimer invalidate]; 
    } 
} 


-(void)flickerAnimation { 
    flickerCount = 0; 
    flickerTimer = [NSTimer scheduledTimerWithTimeInterval:0.3f target:self selector:@selector(flickerOn) userInfo:nil repeats:YES]; 
} 
+1

谢谢JustinXXVII,它似乎在工作。我已经像这样做了! – wh1t3cat1k 2010-11-11 05:48:03

1

所有 UIView的属性都是可以动画的 - 我不相信“高亮显示”或“选定”。

典型地,它是用于非布尔值可用,像“中心”,“阿尔法”,“帧”和“边界”。

尝试调整阿尔法相反,你会看到它的工作。

1

您需要创建的是,当第一个动画完成执行的回调方法。您使用该回调来创建取消选择的动画。请记住,没有像透明度那样的逐渐“选择状态”。你必须使用

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
[UIView beginAnimations:@"animateIn" context:NULL]; 

从苹果的文档:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 

您的方法必须采用下列参数:

animationID

一个NSString包含一个可选的应用程序提供的标识符。这是传递给beginAnimations:context:方法的标识符。这个说法可能是零。

finished

包含布尔值的NSNumber对象。如果动画在停止前运行完成,则值为YES,否则为NO。

上下文

可选应用程序提供的上下文。这是传递给beginAnimations:context:方法的上下文数据。这个说法可能是零。

当动画结束时,回调animationDidStop被调用并传入字符串@“animateIn”。您可以使用该方法来检查哪个动画被调用并在那里处理。

+0

我试过了,但它不起作用。看起来像在执行动画之前调用“animationDidStop”方法。没有更改可见。请看看更新后的帖子。 – wh1t3cat1k 2010-11-09 20:24:51

+0

该代码看起来很完美,但[UIColor whiteColor]和+ blackColor可能不是“可生成的”。如果你只是对“闪烁”感兴趣,那么这不是一个真正的“动画”。你会更适合于改变颜色的NSTimer。我会为该场景添加一个新答案。在您的动画块中,尝试将animateOut中控件的alpha值更改为0.0f。在animateIn中将其更改为1.0f。将持续时间更改为0.3f,并且可能更明显。 – Justin 2010-11-10 14:02:32