gpt4 book ai didi

ios - UIButton 在 setImage : is called 后不更新它的图像

转载 作者:可可西里 更新时间:2023-11-01 05:46:35 25 4
gpt4 key购买 nike

这就是问题所在 - 我有一个类,它是 UIView(一张卡片)的子类。它在自身上创建一个按钮并调用 changeStateIndicator: 当用户触摸它时。

然后卡片必须在按下按钮后翻转并且按钮应该改变它的颜色(实际上是图像)。但它根本没有发生,所以翻转在按钮改变之前开始。这是我的代码:

//Adding a button to my view
UIButton *changeStateButton = [UIButton buttonWithType:UIButtonTypeCustom];
[changeStateButton setImage:[UIImage imageNamed:imageToInitWith] forState:UIControlStateNormal];
[changeStateButton setImage:[UIImage imageNamed:imageToInitWith] forState:UIControlStateHighlighted];
changeStateButton.frame = CGRectMake(0, 0, 30, 30);
changeStateButton.center = CGPointMake(self.bounds.size.width/2+([myWord length]*8)+17, 35);
changeStateButton.tag = 77;
[changeStateButton addTarget:self action:@selector(changeStateIndicator:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:changeStateButton];

//Method which is called when the button is touched
- (void)changeStateIndicator:(UIButton *)sender
{
[sender setImage:[UIImage imageNamed:@"StateYellow"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:@"StateYellow"] forState:UIControlStateHighlighted];
currentWord.done = [NSNumber numberWithInt:currentWord.done.intValue-10];
[self prepareForTest];
}

//Method which flips the card
- (void)prepareForTest
{
testSide = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cal_small3"]];
[UIView transitionFromView:self toView:testSide duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
}

我的猜测是发件人在方法 -changeStateIndicator 结束之前不会更改其图像,但我不太确定这是实际问题。请帮助我。

最佳答案

尝试推迟这个调用:

dispatch_async(dispatch_get_main_queue(), ^{ [self prepareForTest]; });

如果这种方法有效但仍然不是您想要的,那么使用 dispatch_after() 或

[self performSelector:@selector(prepareForTest) withObject:nil afterDelay:0.25f];

(如果需要,您可以使用时间为“0”的第二次调用而不是调度 - 同样的事情。)

关于ios - UIButton 在 setImage : is called 后不更新它的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11639551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com