Objective c 比较新旧观察值,但函数是否未按预期工作
我有两种淡入淡出的方法,可以简单地改变文本字段的背景。唯一的区别是用于背景的图像。图像的名称已被复制和粘贴,因此没有拼写错误Objective c 比较新旧观察值,但函数是否未按预期工作,objective-c,Objective C,我有两种淡入淡出的方法,可以简单地改变文本字段的背景。唯一的区别是用于背景的图像。图像的名称已被复制和粘贴,因此没有拼写错误 - (IBAction)fadeEnable:(UITextField *)textField; { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1.00]; [textField setAlpha:0]; [UIView commitAnimations]; text
- (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];
}
我的文本字段上有一个观察者,它查看“enabled@属性是否已更改
我试图编写一段代码,说明如果enabled属性更改,它将更改为enabled,然后运行enabled动画。如果它更改为disabled,则运行disabled动画
不幸的是,它只在两种情况下运行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];
}
}
我发现了错误,对于fadeDisabled,我需要更改禁用的背景,而不是正常的背景 使用了以下功能,现在似乎工作正常
textField.disabledBackground = [UIImage imageNamed:@"background_for_text_2"];
检查调试器(或使用log语句)是否找到图像。特别是,您启用的图像名称具有
.png
扩展名,但禁用的图像名称没有扩展名;除非您只有@2x,否则这应该可以工作,但请尝试删除.png。不确定如何执行。我删除了.png,没有任何区别。