我有 2 种淡入淡出方法,可以简单地改变文本字段的背景。唯一的区别是用于背景的图像。图片的名称已被复制和粘贴,因此没有拼写错误。
- (IBAction)fadeEnable:(UITextField *)textField;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.00];
[textField setAlpha:0];
[UIView commitAnimations];
textField.background = [UIImage imageNamed:@"background_for_text.png"];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.00];
[textField setAlpha:1];
[UIView commitAnimations];
}
- (IBAction)fadeDisable:(UITextField *)textField;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.00];
[textField setAlpha:0];
[UIView commitAnimations];
textField.background = [UIImage imageNamed:@"background_for_text_2"];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.00];
[textField setAlpha:1];
[UIView commitAnimations];
}
我的 textField 上有一个观察者,它查看“启用@ 属性是否已更改。
我正在尝试编写一些代码,说明如果启用的属性发生更改,并且它更改为启用,然后运行启用的动画。如果它更改为禁用,则运行禁用的动画。
不幸的是,它只在两种情况下都运行 fadeDisabled 方法,我不知道为什么。
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *) context;
{
UITextField *txtField = (UITextField *)object;
BOOL new = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
BOOL old = [[change objectForKey:NSKeyValueChangeOldKey] boolValue];
NSLog(@"new,%i",new);
NSLog(@"old,%i",old);
if ((new != old) && (new == NO))
{
[self fadeDisable:txtField];
}
else if ((new != old) && (new == YES))
{
[self fadeEnable:txtField];
}
}